15

I want to simplify a fraction in my application. The fraction is like, x/y where x and y are integers. I want to simplify the fraction to its simplest form. Can anyone please give me hints how to do it. Thanks in advance.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 2
    For future reference note that [std::gcd](https://en.cppreference.com/w/cpp/numeric/gcd) is available since c++17 – acraig5075 Jan 23 '20 at 12:59

3 Answers3

34
  • Compute the greatest common divisor for x and y
  • Divide both of them by the GCD

Euclid's algorithm is an easy way to compute the GCD.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Note that since C++17, there is [std::gcd](https://en.cppreference.com/w/cpp/numeric/gcd), so you don't have to roll your own. – Raven May 12 '23 at 09:35
20

Divide both by gcd(x,y)

The Binary GCD algorithm is a fast way to compute the GCD on a computer.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
-4
     #include<iostream>
      using namespace std;
        struct fraction
        {
              int n1, d1, n2, d2, s1, s2;
             };
         void simplification(int a,int b)
       {
                bool e = true;
            int t; int z;
        for (int i = (a*b); i > 1;i--)
        { if ((a%i==0)&&(b%i==0))
       {
        t = a / i;
      z = b / i;

         }
else
         {
            e = false;
       }
         }
         cout << "simplest form=" << t << "/" << z << endl;

      }
      void sum(int num1, int deno1, int num2, int deno2)
        {
            int k,y;
          k = num1* deno2 + num2*deno1;
          y = deno2*deno1;
           cout << "addition of given fraction = " << k << "/" << y << endl;
         simplification(k, y);
      }
     void sub(int num1, int deno1, int num2, int deno2)
     {              
           int k, y;

            k = num1*deno2 - num2*deno1;
        y = deno1*deno2;
            cout << "Substraction of given fraction = " << k << "/" << y << endl;

          }
         void mul(int num1, int deno1, int num2, int deno2)
            {
            int k, y;

            k = num1*num2;
                y = deno1*deno2;
                 cout << "multiplication of given fration= " << k<< "/" <<y;                                        cout<< endl;
                simplification(k, y);
            }

        void div(int num1, int deno1, int num2, int deno2)
          {
          int k, y;
       ;
     k = num1*deno1;
     y = deno1*num2;
        cout << "division of given fraction" << k << "/" << y << endl;
    simplification(k, y);
     }


      int main()
       {    fraction a;
            cout << "enter numirator of f1=";cin >> a.n1;
          cout << "enter denominator of f1=";cin >> a.d1;
            cout << "enter numirator of f2=";cin >> a.n2;
        cout << "enter denominator of f2=";cin >> a.d2;
            cout << "f1= " << a.n1 << "/" << a.d1 << endl;
                cout << "f2= " << a.n2 << "/" << a.d2 << endl;
            mul(a.n1, a.d1, a.n2, a.d2);
        div(a.n1, a.d1, a.n2, a.d2); 
                sub(a.n1, a.d1, a.n2, a.d2);
                sum(a.n1, a.d1, a.n2, a.d2);
         system("pause");
           }
  • Welcome to SO. Please provide some context to your answer, as code only answers do not fulfill SO standards. And please reformat your code. See http://stackoverflow.com/help/how-to-answer – Uwe Allner Feb 17 '17 at 09:20