How do you Divide in C?

This is a discussion on How do you Divide in C? within the C Programming forums, part of the General Programming Boards category; I Wrote a program in which you enter the number of games played and number of games won in a ...

  1. #1
    Registered User
    Join Date
    Sep 2010

    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

    #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: ");
    printf("Enter games won for League1: ");
    printf("\nEnter games played for League2: ");
    printf("Enter games won for League2: ");
    printf("\nEnter games played for League3: ");
    printf("Enter games won for League3: ");
    printf("\nEnter games played for League4: ");
    printf("Enter games won for League4:");
    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
    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
    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.:
    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
    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-18-2010, 12:18 AM
  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, 04: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
    Replies: 6
    Last Post: 11-22-2002, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21