-3
#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..?

Vishnu CS
  • 748
  • 1
  • 10
  • 24
  • 1
    so many duplicates: [Division result is always zero](https://stackoverflow.com/q/2345902/995714), [Integer division always zero](https://stackoverflow.com/q/9455271/995714), [calculation in C program always results in 0 whenever using division](https://stackoverflow.com/q/7720078/995714)... – phuclv Mar 26 '20 at 05:34

3 Answers3

0
       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
srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
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);
}
0

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.