# Thread: How do you Divide in C?

1. ## How do you Divide in C?

I Wrote a program in which you enter the number of games played and number of games won in a sport (User Enters Number).

Im trying to get the winning percentage to output aswell.

How do I Divide Numbers in C? When I try, i get 0.0000

Code:
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"

int main()
{
int p1,w1,p2,w2,p3,w3,p4,w4,percent1,percent2,percent3,percent4;
printf("Enter games played for League1: ");
p1=GetInteger();
printf("Enter games won for League1: ");
w1=GetInteger();

printf("\nEnter games played for League2: ");
p2=GetInteger();
printf("Enter games won for League2: ");
w2=GetInteger();

printf("\nEnter games played for League3: ");
p3=GetInteger();
printf("Enter games won for League3: ");
w3=GetInteger();

printf("\nEnter games played for League4: ");
p4=GetInteger();
printf("Enter games won for League4:");
w4=GetInteger();

percent1=(w1/p1)*100;
percent2=(w2/p2)*100;
percent3=(w3/p3)*100;
percent4=(w4/p4)*100;

printf("Name"); printf("	Games Played"); printf("	Games Won"); printf("	Winning Percent");
printf("\n-------"); printf("  -------------"); printf("	----------"); printf("	-------------\n");
printf("\nLeague1"); printf("		%d",p1); printf("		%d",w1); printf("	   %f",percent1);
printf("\nLeague2"); printf("		%d",p2); printf("		%d",w2); printf("	   %f",percent2);
printf("\nLeauge3"); printf("		%d",p3); printf("		%d",w3); printf("	   %f",percent3);
printf("\nLeauge4"); printf("		%d",p4); printf("		%d",w4); printf("	   %f\n",percent4);
}

2. Percent means hundreths! So you can't use integers for this. Use floats and %f to enter and print them, is the format.

Does your Mother know you're trying to work with hundreths, using integers?

3. When you divide an integer by another integer, the result is an integer and any fractional part is truncated (discarded), so if as in your case the quotient is between 0 and 1 the result will be 0. Also, when you assign the result of any calculation to an integer variable, the result is truncated.

You can solve your immediate problem by casting either the numerator or denominator to a double, e.g.:
Code:
percent1 = (double)w1/p1*100;
but you will still have to deal with the other problem -- as it is, percent1 will always be rounded down.

4. You declared percent1, percent2, percent3, and percent4 as integers.