int percent (int a, int b)
{
if (b/a*100 > 25)
return TRUE;
else
return FALSE;
}
I want to calculate what percentage of a is b.
say if b = 48, a = 100
so b is 48% of a
but wouldnt b/a give me 0 ??? what can be done ??
int percent (int a, int b)
{
if (b/a*100 > 25)
return TRUE;
else
return FALSE;
}
I want to calculate what percentage of a is b.
say if b = 48, a = 100
so b is 48% of a
but wouldnt b/a give me 0 ??? what can be done ??
percent=a100/b
With integer arithemetic you must sequence this as (a100)/b
u mean (b*100) /a ??
but then supposed (b100)/a is 25.5 which is greater than 25. but still the condition (b/a100 > 25) will evaluate to false right ?
Use float
int percent (float a, float b)
Since you declared variables a and b as int , expression (b*100) /a is evaluated and rounded.
yes, yes.. I know, thats where the problem is, a and b are int
Why dont u store the division result in the float variable ... I guess compiler will auto cast to higher ... Just check with this
float f:
f= b/a * 100;
and then checking with 25 ...
since a and b are int b/a will return 0.
So, b/a * 100 will return 0. But since f is float, f will be 0.0
I think this works:
f = ((float)b * 100)/a