This is a discussion on simple question about % within the C Programming forums, part of the General Programming Boards category; Hi, how would you code finding the percentage of one number and assigning that value to a variable. Like finding ...

1. ## simple question about %

Hi,
how would you code finding the percentage of one number and assigning that value to a variable. Like finding 15% of 150 and assigning that to
Code:
`int percent;`
I think you have to write it as 0.15, not 15% ? ?

thanks

2. Don't you know how to turn a fraction into a percentage? This is fairly basic math.

Quzah.

3. Once you figure out the math, perhaps you'll notice that type "int" won't really cut it.

4. Im just not sure how to code it.

5. Make your best effort, get it to compile, and post your code.

6. Code:
```#include<stdio.h>

int value;
float percentage,output;

value = 150;
percentage = 0.15;

int main()
{
output = value * percentage;
printf("\n Result = %.2f \n", output);
}```
Would something like that work ?

I get an output but I also get a compile error, something about having to truncate a double to a floating point.

7. This is primary school maths. If you want to find 15% of X, multiple X by 15 and then divide the result of that by 100.

In C, as with a lot of programming languages, if you do it with integer variables, rounding will occur. For example, 15% of 50 is actually 7.5. But if the value of 50 is of type int, the result will be 7 (7.5 can not be represented as an integer).

8. As for your compiler error . . .
Code:
```#include<stdio.h>

int value;
float percentage,output;

value = 150;
percentage = 0.15;

int main()
{
output = value * percentage;
printf("\n Result = %.2f \n", output);
}```
You can't have executable code outside of a function (at least not that kind). Change it to this:
Code:
```#include<stdio.h>

int value;
float percentage,output;

int main()
{
value = 150;
percentage = 0.15;
output = value * percentage;
printf("\n Result = %.2f \n", output);
}```
Or, better yet, use local variables:
Code:
```#include<stdio.h>

int main()
{
int value;
float percentage,output;

value = 150;
percentage = 0.15;

output = value * percentage;
printf("\n Result = %.2f \n", output);
}```
Or, best of all, use initialization instead of assignment:
Code:
```#include<stdio.h>

int main()
{
int value = 150;
float percentage = 0.15,output;

output = value * percentage;
printf("\n Result = %.2f \n", output);

return 0;  /* remember to include this, too */
}```

This is primary school maths. If you want to find 15% of X, multiple X by 15 and then divide the result of that by 100.
Or better yet, multply X by .15, which is what [s]he's doing.

In C, as with a lot of programming languages, if you do it with integer variables, rounding will occur. For example, 15% of 50 is actually 7.5. But if the value of 50 is of type int, the result will be 7 (7.5 can not be represented as an integer).
This is irrelevent. The result is stored in a float (which would be better of as a double, but still).

I think (his|her) math is fine.
[/edit]

9. Originally Posted by dwks
Thanks for fixing my compile error, thats going to come in handy for my course when/if I come accross that problem again I will ken how to fix it.

As for my maths, I`m correct in multiplying by the decimal of the percentage value I`m looking for. Not sure what you mean by my math problem?

10. Ok, I was a little bored and this kind of interested me [ seeing as I`m doing a C programming course it is something for me to practise on ].

Had a little play around and came up with a very simple percentage calculator that does the maths for the user, all they have to do is punch in numbers.

PHP Code:
``` #include<stdio.h> /*create variables*/  float value, percentage, result;   int main() {     /*Give user some idea of what it is they are using*/     printf("\t\t**SIMPLE PERCENTAGE VALUE CALCULATOR**\n");     /*Inform user how to enter data*/     printf("\nPlease enter the values you wish to be calculated in the following format:");     printf("\n[VALUE], [PERCENTAGE]\n");     /*Capture data*/     scanf_s("%f,%f", &value, &percentage);          /*Do the maths*/     result = value * (percentage/100);     /*Produce a result*/     printf("\n Result = %.2f \n", result); }  ```
I put the values as floating point numbers just incase the user has some wierd and/or wonderful numbers to work with. Not sure if I have set up the variables correctly but I didn't get any compile errors so I am assuming my code is fine. Perhaps not formatted like a pro, but I`m not a pro so I`m not going to worry yet.

Is there anything else that could be done to it ? Perhaps a switch statement so the user can exit without using the cross window buttons ?

Sorry if I have taken over this thread by the way, I ken its not mine, just kinda interested me and appear to be going in the right direction with what the original poster wanted.