Hollowfires

Untitled

Dec 3rd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. class Rational
  8. {
  9. private:
  10. void reduce();
  11. //numerator variable
  12. int num;
  13. //denominator variable
  14. int denom;
  15.  
  16. public:
  17. Rational(int n, int d)
  18. {
  19. num = n;
  20. denom = d;
  21. reduce();
  22. }
  23.  
  24. int gcd(int n, int d)
  25. {
  26. if ( n % d == 0 ) // base case
  27. return d;
  28. else
  29. return gcd(d, n % d);
  30. }
  31.  
  32.  
  33. void reduce(int &n, int &d)
  34. {
  35. n = n/gcd;
  36. d = d/gcd;
  37.  
  38. }
  39.  
  40. };
  41.  
  42.  
  43.  
  44. int main()
  45. {
  46. int num;
  47. int denom;
  48.  
  49. cout << "Enter a numerator. \n";
  50. cin >> num;
  51. cout << "\n Enter a denominator. \n";
  52. cin >> denom;
  53.  
  54. Rational fraction(num, denom);
  55.  
  56. fraction.gcd(num, denom);
  57.  
  58. fraction.reduce(num, denom);
  59.  
  60. cout << num << "/" << denom;
  61.  
  62. return 0;
  63.  
  64.  
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment