Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class Rational
- {
- private:
- void reduce();
- //numerator variable
- int num;
- //denominator variable
- int denom;
- public:
- Rational(int n, int d)
- {
- num = n;
- denom = d;
- //reduce();
- }
- int gcd(int n, int d)
- {
- if ( n % d == 0 ) // base case
- return d;
- else
- return gcd(d, n % d);
- }
- void reduce(int n, int d, int &gcd)
- {
- n = n/gcd;
- d = d/gcd;
- }
- };
- int main()
- {
- int num = 0;
- int denom = 1;
- int gcd;
- cout << "Enter a numerator. \n";
- cin >> num;
- cout << "\nEnter a denominator. \n";
- cin >> denom;
- Rational fraction(num, denom);
- fraction.gcd(num, denom);
- fraction.reduce(num, denom, gcd);
- cout << num<< "/" << denom;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment