In: Computer Science
C++ Code :
#include <bits/stdc++.h>
using namespace std;
struct Fraction// name of the structure storing numerator and
denominator
{
int n;//numerator
int d;//denominator
};
void printFraction(Fraction f) // this function is to print the
fraction in " n / d " form
{
cout<< (f.n) << "/" << (f.d) << "\n"; //
"." is used to make a reference to the variable n and d
}
Fraction mult(Fraction f1, Fraction f2)//this function is to
multiply the two fractions
{
Fraction f3;//multiplication is n*n / d*d
f3.n = f1.n * f2.n;
f3.d = f1.d * f2.d;
return f3;
}
Fraction add(Fraction f1, Fraction f2)//this function is to add the
two fractions
{
Fraction f3;
f3.n = (f1.n * f2.d ) + (f2.n * f1.d);//this is by easy lcm
method
f3.d = f1.d * f2.d;
return f3;
}
int main()
{
int a,b;
cout<<"Enter the numerator of a fraction : ";
cin>>a;
cout<<"Enter the denominator of a fraction : ";
cin>>b;
Fraction f;
f.n=a;
f.d=b;
cout<<"The value of fraction is : ";
printFraction(f);
Fraction f1;
Fraction f2;
cout<<"Enter the numerator of a fraction 1: ";
cin>>a;
cout<<"Enter the denominator of a fraction 1: ";
cin>>b;
f1.n = a;
f1.d = b;
cout<<"Enter the numerator of a fraction 2: ";
cin>>a;
cout<<"Enter the denominator of a fraction 2: ";
cin>>b;
f2.n = a;
f2.d = b;;
f=mult(f1,f2);//multiply two fraction and store in f
cout<<"The value of fraction after muliplication is :
";
printFraction(f);
f=add(f1,f2);//add two fraction and store in f
cout<<"The value of fraction after addition is : ";
printFraction(f);
return 0;
}
The output is also shown here :
If you are satisfied by the explanation given through the comments in the code Then Please Upvote my answer.