Thread: How do you Divide in C?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    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. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You declared percent1, percent2, percent3, and percent4 as integers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-17-2010, 11:18 PM
  2. Replies: 15
    Last Post: 01-24-2008, 09:40 PM
  3. Divide 64bit number
    By freeindy in forum C Programming
    Replies: 4
    Last Post: 10-01-2007, 03:56 AM
  4. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 07:31 PM
  5. severe beating of divide by zero
    By muttski in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-22-2002, 05:54 PM