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
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
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.
yea thanx
but i want the result like 1/3+2/7 =13/21
i want the result is 13/21
Then simply have 2 sum values, one for the numerator and one for the denominator like so:
And then use printf() like so:Code:sum_nom=((nump1*denom2)+(nump2*denom1)); sum_denom=(denom1*denom2);
Code:printf("Sum is: %d/%d\n",sum_nom,sum_denom);
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
this also add two fractions but not for all fractions. plz tell me what is the problem..........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; } }