/*
State the output
*/
#include<iostream>
#include<cstdlib>
using namespace std;
class Fraction
{
public:
int num,deno;
Fraction()
{
num=1;
deno=1;
}
Fraction(int n,int d)
{
num=n;
if(d==0)
{
cout<<"\n Error: Attempting to Divide by Zero";
exit(0);
}
else
{
deno=d;
}
}
Fraction operator+(Fraction f)
{
int n=num*f.deno+f.num*deno;
int d=deno*f.deno;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator-(Fraction f)
{
int n=num*f.deno-f.num*deno;
int d=deno*f.deno;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator*(Fraction f)
{
int n=num*f.num;
int d=deno*f.deno;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator/(Fraction f)
{
int n=num*f.deno;
int d=deno*f.num;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
bool operator==(Fraction &f)
{
return (num==f.num && deno==f.deno);
}
int gcd(int n,int d)
{
int rem;
while(d!=0)
{
rem=n%d;
n=d;
d=rem;
}
return n;
}
void accept()
{
cout<<"\n\n Enter Numerator: ";
cin>>num;
cout<<"\n\n Enter Denominator: ";
cin>>deno;
}
};
int main()
{
Fraction f1,f2,f3;
cout<<"\n Enter 1st Fractional Value ";
cout<<"\n ***************************";
f1.accept();
cout<<"\n Enter 2nd Fractional Value ";
cout<<"\n ***************************";
f2.accept();
f3=f1+f2;
cout<<"\n*****************************";
cout<<"\n Sum of two Numbers "<<f3.num<<f3.deno;
f3=f1-f2;
cout<<"\n*****************************";
cout<<"\n Difference of two Numbers "<<f3.num<<f3.deno;
f3=f1*f2;
cout<<"\n*****************************";
cout<<"\n Product of two Numbers "<<f3.num<<f3.deno;
f3=f1/f2;
cout<<"\n*****************************";
cout<<"\n Division of two Numbers "<<f3.num<<f3.deno;
cout<<"\n*****************************";
if(f1==f2)
cout<<"\n Fraction is Equal to Fraction 2";
else
cout<<"\n Fraction is Not Equal to Fraction 2";
return 0;
}
State the output
*/
#include<iostream>
#include<cstdlib>
using namespace std;
class Fraction
{
public:
int num,deno;
Fraction()
{
num=1;
deno=1;
}
Fraction(int n,int d)
{
num=n;
if(d==0)
{
cout<<"\n Error: Attempting to Divide by Zero";
exit(0);
}
else
{
deno=d;
}
}
Fraction operator+(Fraction f)
{
int n=num*f.deno+f.num*deno;
int d=deno*f.deno;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator-(Fraction f)
{
int n=num*f.deno-f.num*deno;
int d=deno*f.deno;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator*(Fraction f)
{
int n=num*f.num;
int d=deno*f.deno;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction operator/(Fraction f)
{
int n=num*f.deno;
int d=deno*f.num;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
bool operator==(Fraction &f)
{
return (num==f.num && deno==f.deno);
}
int gcd(int n,int d)
{
int rem;
while(d!=0)
{
rem=n%d;
n=d;
d=rem;
}
return n;
}
void accept()
{
cout<<"\n\n Enter Numerator: ";
cin>>num;
cout<<"\n\n Enter Denominator: ";
cin>>deno;
}
};
int main()
{
Fraction f1,f2,f3;
cout<<"\n Enter 1st Fractional Value ";
cout<<"\n ***************************";
f1.accept();
cout<<"\n Enter 2nd Fractional Value ";
cout<<"\n ***************************";
f2.accept();
f3=f1+f2;
cout<<"\n*****************************";
cout<<"\n Sum of two Numbers "<<f3.num<<f3.deno;
f3=f1-f2;
cout<<"\n*****************************";
cout<<"\n Difference of two Numbers "<<f3.num<<f3.deno;
f3=f1*f2;
cout<<"\n*****************************";
cout<<"\n Product of two Numbers "<<f3.num<<f3.deno;
f3=f1/f2;
cout<<"\n*****************************";
cout<<"\n Division of two Numbers "<<f3.num<<f3.deno;
cout<<"\n*****************************";
if(f1==f2)
cout<<"\n Fraction is Equal to Fraction 2";
else
cout<<"\n Fraction is Not Equal to Fraction 2";
return 0;
}