28

I am trying to solve:

int total=0, number=0;
float percentage=0.0;

percentage=(number/total)*100;
printf("%.2f", percentage);

If the value of the number is 50 and the total is 100, I should get 50.00 as percentage and that is what I want. But I keep getting 0.00 as the answer and tried many changes to the types but they didn't work.

Sakib Arifin
  • 255
  • 2
  • 13
Jack
  • 341
  • 1
  • 3
  • 4

11 Answers11

39

Integer division truncates, so (50/100) results in 0. You can cast to float (better double) or multiply with 100.0 (for double precision, 100.0f for float precision) first,

double percentage;
// ...
percentage = 100.0*number/total;
// percentage = (double)number/total * 100;

or

float percentage;
// ...
percentage = (float)number/total * 100;
// percentage = 100.0f*number/total;

Since floating point arithmetic is not associative, the results of 100.0*number/total and (double)number/total * 100 may be slightly different (the same holds for float), but it's extremely unlikely to influence the first two places after the decimal point, so it probably doesn't matter which way you choose.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • The former performs the calculation with double precision, the latter with single precision. So they may not be equivalent. Use `100.0f` to get a float literal. – Lundin Nov 23 '12 at 14:23
10

integer division in C truncates the result so 50/100 will give you 0

If you want to get the desired result try this :

((float)number/total)*100

or

50.0/100
Omkant
  • 9,018
  • 8
  • 39
  • 59
3

I routinely multiply by 1.0 if I want floating point, it's easier than remembering the rules.

Alan Corey
  • 577
  • 6
  • 10
2

No, because you do the expression using integers, so you divide the integer 50 by the integer 100, which results in the integer 0. Type cast one of them to a float and it should work.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

You are doing integer arithmetic, so there the result is correct. Try

percentage=((double)number/total)*100;

BTW the %f expects a double not a float. By pure luck that is converted here, so it works out well. But generally you'd mostly use double as floating point type in C nowadays.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

This should give you the result you want.

double total = 0;
int number = 0;
float percentage = number / total * 100
printf("%.2f",percentage);

Note that the first operand is a double

ALOToverflow
  • 2,679
  • 5
  • 36
  • 70
0

Change your code to:

int total=0, number=0;
float percentage=0.0f;

percentage=((float)number/total)*100f;
printf("%.2f", (double)percentage);
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

This can give you the correct Answer

#include <stdio.h>
int main()
{
    float total=100, number=50;
    float percentage;
    percentage=(number/total)*100;
    printf("%0.2f",percentage);
    return 0;
}
0

Just Make the number variable int to float.

int total=0, number=0;
float percentage=0.0;

percentage=((float)number/total)*100;
printf("%.2f", percentage);

Add (float) before the variable name when you use.

Hayt
  • 5,210
  • 30
  • 37
0
#include <stdio.h>
#include <conio.h>

int main() {

    int i,ortust=0,ortalt=0,toplam,dizi[5],sayac;
    float ortalama;
    
    for(i=0;i<5;i++)
    {
        printf("%d. sayiyi giriniz ",i++);
        scanf("%d",&dizi[i]);
        sayac++;
        toplam+=dizi[i];
    }
    
    ortalama=(float)(toplam/sayac);
    for(i=0;i<5;i++){
        if(dizi[i]<ortalama){
            ortalt++;
        }
        else if (dizi[i]>ortalama){
            ortust++;
        }
    }
    printf("Ortalama: %2.f\n",ortalama);
    printf("Ortalamanin altinda kalan eleman sayisi: \n ",ortalt);
    printf("Ortalamanin ustunde kalan eleman sayisi: ",ortust); 
    
    
    

    getch();
    return 0;
}
James Risner
  • 5,451
  • 11
  • 25
  • 47
0

The order matters. You can't just put a double somewhere in the expression. The parser has to come across it before it does any integer arithmetic.

int obs, tot;
float percent;
obs = 17;
tot = 40;
printf("%f\n",
        percent = obs / tot * 100);
printf("%f\n",
        percent = obs / tot * 100.0);
printf("%f\n",
        percent = 100.0 * obs / tot);
printf("%f\n",
        percent = obs*100.0/ tot);
printf("%f\n",
        percent = 100.0* (obs / tot) );

Gives output:

0.000000
0.000000
42.500000
42.500000
0.000000

So the double has to appear in the first operation to be performed. The third and fourth methods work OK but note that parentheses can prioritise the integer division, so fifth fails.