Like Tree2Likes
  • 2 Post By anduril462

problem of add two fractions

This is a discussion on problem of add two fractions within the C Programming forums, part of the General Programming Boards category; i have a problem to print out the sum of the fraction i cannot printf anything of it printf("Please enter ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    problem of add two fractions

    i have a problem to print out the sum of the fraction

    i cannot printf anything of it

    printf("Please enter the first number\n");
    fgets(line, sizeof(line),stdin);
    sscanf(line,"%d%d",&nump1, &demon1);
    //puts(line);
    printf("Please enter the second number\n");
    fgets(line, sizeof(line),stdin);
    sscanf(line,"%d%d",&nump2, &demon2);
    //puts(line);
    sum=((nump1*demon2)+(nump2*demon1))/(demon1*demon2);

    i think i am doing a right track but not sure why cannot printout

    and after get the result
    what is the best way to get lowest common denominator

    thanx

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    I'll correct your english too:

    i have a problem printing out the sum of the fraction.

    i can not use printf to print the output

    Code:
    printf("Please enter the first number\n");
    fgets(line, sizeof(line),stdin);
    sscanf(line,"%d%d",&nump1,  &demon1);
    //puts(line);
    printf("Please enter the second number\n");
    fgets(line, sizeof(line),stdin);
    sscanf(line,"%d%d",&nump2,  &demon2);
    //puts(line);
    sum=((nump1*demon2)+(nump2*demon1))/(demon1*demon2);
    i think i am on the right track, but i'm not sure why i can not print out the result

    ...and after get i the result, what is the best way to get lowest common denominator?

    thanx

    -------------------------------------------------------------------------

    try adding this line:

    printf("The sum is %d", sum);

    i bet you were trying
    printf(sum);

    you need the "%d"
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    hi thanx for reply

    i did add this line printf("The sum is %d", sum);

    i forgot to post it


    it displayed what i dont suppose

    help

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Make sure you use float-pointing divide instead of integer division:

    Integer division examples:
    1/3 = 0
    4/3 = 1
    9/3 = 3

    Floating-Point division examples:
    1.0/3.0 = 0.33..
    4.0/3.0 = 1.33..
    9.0/3.0 = 3.0

    Also you'll need to change your type from integer to either float or double (use %f for float).
    Last edited by 0rion; 04-04-2005 at 05:00 AM.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    yea thanx

    but i want the result like 1/3+2/7 =13/21

    i want the result is 13/21

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Then simply have 2 sum values, one for the numerator and one for the denominator like so:
    Code:
    sum_nom=((nump1*denom2)+(nump2*denom1));
    sum_denom=(denom1*denom2);
    And then use printf() like so:
    Code:
    printf("Sum is: %d/%d\n",sum_nom,sum_denom);

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    That won't give the simplified answer though 0rion. To do that you need to look up gcm algorithms.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Lahore, Pakistan, Pakistan
    Posts
    2
    Code:
    #include<iostream.h>
    #include<math.h>
    struct fractions
    {
    	 float a,b,c,d,e,f;
    };
    
    float sum (float u,float v);
    int GCD(int a, int b);
    int main (void)
    {
     fractions f1;
     cout<<"Enter Integer value of numerator of first fraction= ";
     cin>>f1.a;
     cout<<"Enter Integer value of dinominator of first fraction= ";
     cin>>f1.b;
     cout<<"Hence first fraction= "<<f1.a<<"/"<<f1.b<< endl;
     cout<<"Enter Integer value of numerator of second fraction= ";
     cin>>f1.d;
     cout<<"Enter Integer value of dinominator of second fraction= ";
     cin>>f1.e;
     cout<<"Hence second fraction= "<<f1.d<<"/"<<f1.e<< endl;
     f1. c = f1.a/f1.b;
     f1. f = f1.d/f1.e;
     float result=sum(f1.c,f1.f);
     int i=result*pow(10,3);
     int j=pow(10,3);
     int k=GCD(i,j);
     i=i/k;
     j=j/k;
     cout<<f1.a<<"/"<<f1.b<<" + "<<f1.d<<"/"<<f1.e<<" = "<<i<<"/"<<j<<endl;
    
     return 0;
     }
     float sum (float u,float v)
     {
    	 float w;
    	 w=u+v;
        return w;
     }
     int GCD(int a, int b)
    {
    	 while( 1 )
    	 {
    		  a = a % b;
    		if( a == 0 )
    			return b;
    		b = b % a;
    
    		  if( b == 0 )
    			return a;
    	 }
    }
    this also add two fractions but not for all fractions. plz tell me what is the problem..........

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,437
    The problem is you didn't read the forum guidelines. Don't bump old threads. Start your own thread, then you tell us what the problem is, and we'll help you fix it.

    EDIT: And you're writing C++, but posting in the C forum. Pick the right forum for your post.
    quzah and Jasir Ali like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with unaligned intrinsics
    By The Wazaa in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2009, 11:36 AM
  2. Linked List, a pointer problem?
    By SkidMarkz in forum C Programming
    Replies: 3
    Last Post: 09-16-2008, 10:39 AM
  3. Problem about making setup project
    By sgh in forum Windows Programming
    Replies: 0
    Last Post: 04-30-2008, 03:09 AM
  4. Add and delete functions
    By Ana Val sazi in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2002, 09:59 PM
  5. Can somebody test this code please
    By andy bee in forum C Programming
    Replies: 6
    Last Post: 10-09-2001, 03:08 PM

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