If
you have two fractions, a/b and c/d, their sum can be obtained from the
formula: a/b+c/d = a*d+b*c/b*d.
For
example, 1/4plus2/3 is:
1/4+2/3 = 1*3+4*2/4*3=3+8/12=11/12
Write
a program so that all fractions are stored in variables of type struct fraction, whose two members are
the fraction’s numerator and denominator (both type int). All fraction related
data should be stored in structures of this type.
SOLUTION:
#include<iostream.h>
#include<conio.h>
/////////////////////////////////////////////////////////////
Define Structures
////////////////////////////////////////////////////////////
struct fraction
{
int nume,denom;
};
///////////////////////////////////////////////////// Define Structure Variables
/////////////////////////////////////////////////////
fraction f1,f2,f3;
void main()
{
char dummychar;
clrscr();
/////////////////////////////////////////////////////////////////
Take Input
////////////////////////////////////////////////////////////////////
cout<<"Enter First Fraction:
";
cin>>f1.nume>>dummychar>>f1.denom;
cout<<"Enter Second Fraction:
";
cin>>f2.nume>>dummychar>>f2.denom;
//////////////////////////////////////////////////////////////////
Calculate f3
////////////////////////////////////////////////////////////////
f3.nume=(f1.nume*f2.denom)+(f1.denom*f2.nume);
f3.denom=f1.denom*f2.denom;
//////////////////////////////////////////////////////////////////////
Output /////////////////////////////////////////////////////////////////////
cout<<"\n"<<f1.nume<<"/"<<f1.denom<<"
+ "<<f2.nume<<"/"<<f2.denom<<" =
";
cout<<f3.nume<<"/"<<f3.denom<<"\n";
getch();
}
OUTPUT:
0 comments: