-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRational.cpp
51 lines (51 loc) · 900 Bytes
/
Rational.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
using namespace std;
class rational
{
int nem,deno;
public:
rational()
{
nem=0;
deno=1;
}
rational(int a,int b)
{ nem=a;
deno=b;
}
void reduce()
{ int gcd;
int rem,i;
int n1=nem,n2=deno;
//GCD
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
nem/=gcd; //Dividing Numerator and Denominator by GCD
deno/=gcd;
}
friend ostream &operator << (ostream &output,rational &p);
friend istream &operator >> (istream &input,rational &p);
};
ostream &operator << (ostream &output,rational &p) //Operator Overloading
{
output<<p.nem<<"/"<<p.deno<<endl;
return output;
}
istream &operator >> (istream &input,rational &p)
{
cout<<"Enter enter a rational num in new & deno form \n";
input>>p.nem>>p.deno;
return input;
}
int main()
{
rational r(8,4);
cin>>r;
r.reduce();
cout<<r;
return 0;
}