# problem of add two fractions

• 04-04-2005
tenseven
i have a problem to print out the sum of the fraction

i cannot printf anything of it

fgets(line, sizeof(line),stdin);
sscanf(line,"%d%d",&nump1, &demon1);
//puts(line);
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
• 04-04-2005
misplaced

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

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

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

i bet you were trying
printf(sum);

you need the "%d"
• 04-04-2005
tenseven

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

i forgot to post it

it displayed what i dont suppose

help
• 04-04-2005
0rion
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).
• 04-04-2005
tenseven
yea thanx

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

i want the result is 13/21
• 04-04-2005
0rion
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);`
• 04-04-2005
chrismiceli
That won't give the simplified answer though 0rion. To do that you need to look up gcm algorithms.
• 05-31-2011
Jasir Ali
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..........
• 05-31-2011
anduril462