#include <stdio.h>
main()
{
int c=2,n=5;
float percent;
percent=(c/n)*100;
printf("%.3f\n",percent);
}
what's wrong in this code please..?
#include <stdio.h>
main()
{
int c=2,n=5;
float percent;
percent=(c/n)*100;
printf("%.3f\n",percent);
}
what's wrong in this code please..?
percent=(c/n)*100;
2/5 ---> 0 because it is integer division
0*100 ---> 0
It is an integer division, So you can change this to
percent=((float)c/n)*100;
2/5 ----> 0.4
0.4*100 ----> 40.0
You are doing integer division. It always truncates the value. Make sure you cast either one first then it will use floating point division.
#include <stdio.h>
int main()
{
int c=2,n=5;
float percent;
percent=((float)c/n)*100;
printf("%.3f\n",percent);
}
Division operation between integers yield another integer. Since c, n & 100 are integers you get an integer as a result with .0 since the type is declared as float. The other answers here should produce your desired answer.