Hollowfires

Untitled

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