Thread: problem of add two fractions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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.

  3. #3
    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

  4. #4
    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);

  5. #5
    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

  6. #6
    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..........

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, 12:36 PM
  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