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 toI think you have to write it as 0.15, not 15% ? ?Code:int percent;
thanks
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 toI think you have to write it as 0.15, not 15% ? ?Code:int percent;
thanks
Last edited by richdb; 01-20-2006 at 06:43 PM.
Don't you know how to turn a fraction into a percentage? This is fairly basic math.
Quzah.
Hope is the first step on the road to disappointment.
Once you figure out the math, perhaps you'll notice that type "int" won't really cut it.
Im just not sure how to code it.
Make your best effort, get it to compile, and post your code.
Would something like that work ?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); }
I get an output but I also get a compile error, something about having to truncate a double to a floating point.
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).
As for your compiler error . . .
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; value = 150; percentage = 0.15; int main() { output = value * percentage; printf("\n Result = %.2f \n", output); }
Or, better yet, use local variables: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, best of all, use initialization instead of assignment: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); }
Of course, this doesn't fix your math, just your compiler errors.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 */ }
[edit]
Or better yet, multply X by .15, which is what [s]he's doing.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.
This is irrelevent. The result is stored in a float (which would be better of as a double, but still).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).
I think (his|her) math is fine.
[/edit]
Last edited by dwks; 01-21-2006 at 03:24 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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.Originally Posted by dwks
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?
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.
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.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);
}
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.