C++ | Program That can Add, subtract, multiply and divide fraction and give answer in simplest form !

So here is the code and the explanation below :

#include <iostream>
using namespace std;

void fractions(int x, int y,int w, int z);
void addi(int x, int y,int w, int z);
void subt(int x, int y,int w, int z);
void multiply(int x, int y,int w, int z);
void division(int x, int y,int w, int z);

main (){
    int num1, den1, num2, den2,dec;
    cout << "Enter Numerator 1:" ;
    cin >> num1;
    cout << "\nEnter Denominator 1:" ;
    cin >> den1;
    cout << "\nEnter Numerator 2:" ;
    cin >> num2;
    cout << "\nEnter Denominator 2:" ;
    cin >> den2;
    fractions(num1, den1, num2, den2);
    cout << "\nEnter 1 for Addition";
    cout << "\nEnter 2 for Subtraction";
    cout << "\nEnter 3 for Multiplication";
    cout << "\nEnter 4 for Division";
    cout << "\nWhat operation you want to do:";
    cin >> dec;
    switch (dec){
       case 1:
            cout << "\nAddition" << endl;
            addi(num1, den1, num2, den2);
            break;
        case 2:
            cout << "\nSubtraction" << endl;
            subt(num1, den1, num2, den2);
            break;
        case 3:
            cout << "\nMultiplication" << endl;
            multiply(num1, den1, num2, den2);
            break;
        case 4:
            cout << "\nDivision" << endl;
            division(num1, den1, num2, den2);
            break;
    default:
            cout << "\nInvalid Input!" << endl;
            break;
    }

    return 0;
}

void fractions(int x, int y,int w, int z){
    cout << "\n\nFraction 1 is " << x << "/" << y << endl;
    cout << "Fraction 2 is " << w << "/" << z << endl;
}

void addi(int x, int y,int w, int z){
    int n, d, m, n2,d2,n3,d3,n5,d5,n7,d7;
    int i = 1;
    n = x*z + w*y; // (n1 x d2) + (n2 x d1) // n is new numerator
    d = y*z; // d1 x d2 // n is new denominator
    m = n%d; // m is modulus of numerator n / denominator d
    if ( m == 0){
        cout << endl << x << "/" << y << " + "<< w << "/" << z << " = " << n/d << endl;
    }
    else {
            while ( i !=0 ) {
                n2 = n%2;
                d2 = d%2;
                n3 = n%3;
                d3 = d%3;
                n5 = n%5;
                d5 = d%5;
                n7 = n%7;
                d7 = d%7;
                if (n2 == 0 && d2 == 0){
                    n = n/2;
                    d = d/2;
                }
                else if (n3 == 0 && d3 == 0){
                    n = n/3;
                    d = d/3;
                }
                else if (n5 == 0 && d5 == 0){
                    n = n/5;
                    d = d/5;
                }
                else if (n7 == 0 && d7 == 0){
                    n = n/7;
                    d = d/7;
                }
                else
                    break;

                i++;
            }
        cout << endl << x << "/" << y << " + "<< w << "/" << z << " = " << n << "/" << d << endl;
    }

}

void subt(int x, int y,int w, int z){
    int n, d, m,n2,d2,n3,d3,n5,d5,n7,d7;
    int i = 1;
    n = x*z - w*y; // numerator = n1 x d2 - n2 x d1
    d = y*z; // denominator = d1 x d2
    m = n%d;
    if ( m == 0){
        cout << endl << x << "/" << y << " - "<< w << "/" << z << " = " << n/d << endl;
    }
    else {
        while ( i !=0 ) { //infinite loop to divide to the smallest fration
            n2 = n%2; //check if the answer is dividable by 2
            d2 = d%2;
            n3 = n%3; //check if the answer is dividable by 3
            d3 = d%3;
            n5 = n%5; //check if the answer is dividable by 5
            d5 = d%5;
            n7 = n%7; //check if the answer is dividable by 7
            d7 = d%7;
            if (n2 == 0 && d2 == 0){ 
                n = n/2;
                d = d/2;
            }
            else if (n3 == 0 && d3 == 0){ 
                n = n/3;
                d = d/3;
            }
            else if (n5 == 0 && d5 == 0){ 
                    n = n/5;
                    d = d/5;
                }
            else if (n7 == 0 && d7 == 0){ 
                n = n/7;
                d = d/7;
            }
            else //if both  can't be divide anymore by 2,3,5,7, the loop will break
                break;

            i++;
        }
        // Show last result of the subtraction  at smallest fractional form
        cout << endl << x << "/" << y << " - "<< w << "/" << z << " = " << n << "/" << d << endl;
    }
}

void multiply(int x, int y,int w, int z){
    int n, d, m, n2,d2,n3,d3,n5,d5,n7,d7;
    int i = 1;
    n = x*w; // (n1 x n2) // n is new numerator
    d = y*z; // d1 x d2 // n is new denominator
    m = n%d; // m is modulus of numerator n / denominator d
    if ( m == 0){
        cout << endl << x << "/" << y << " + "<< w << "/" << z << " = " << n/d << endl;
    }
    else {
            while ( i !=0 ) {
                n2 = n%2;
                d2 = d%2;
                n3 = n%3;
                d3 = d%3;
                n5 = n%5;
                d5 = d%5;
                n7 = n%7;
                d7 = d%7;
                if (n2 == 0 && d2 == 0){
                    n = n/2;
                    d = d/2;
                }
                else if (n3 == 0 && d3 == 0){
                    n = n/3;
                    d = d/3;
                }
                else if (n5 == 0 && d5 == 0){
                    n = n/5;
                    d = d/5;
                }
                else if (n7 == 0 && d7 == 0){
                    n = n/7;
                    d = d/7;
                }
                else
                    break;

                i++;
            }
        cout << endl << x << "/" << y << " x "<< w << "/" << z << " = " << n << "/" << d << endl;
    }

}

void division(int x, int y,int w, int z){
    int n, d, m, n2,d2,n3,d3,n5,d5,n7,d7;
    int i = 1;
    n = x*z; // (n1 x n2) // n is new numerator
    d = y*w; // d1 x d2 // n is new denominator
    m = n%d; // m is modulus of numerator n / denominator d
    if ( m == 0){
        cout << endl << x << "/" << y << " + "<< w << "/" << z << " = " << n/d << endl;
    }
    else {
            while ( i !=0 ) {
                n2 = n%2;
                d2 = d%2;
                n3 = n%3;
                d3 = d%3;
                n5 = n%5;
                d5 = d%5;
                n7 = n%7;
                d7 = d%7;
                if (n2 == 0 && d2 == 0){
                    n = n/2;
                    d = d/2;
                }
                else if (n3 == 0 && d3 == 0){
                    n = n/3;
                    d = d/3;
                }
                else if (n5 == 0 && d5 == 0){
                    n = n/5;
                    d = d/5;
                }
                else if (n7 == 0 && d7 == 0){
                    n = n/7;
                    d = d/7;
                }
                else
                    break;

                i++;
            }
        cout << endl << x << "/" << y << " / "<< w << "/" << z << " = " << n << "/" << d << endl;
    }

}


With just a few tweaks, I got it with multiplication and division. This program will simply accept two fractions and do the operation you want it to. I write in primitive programming instead of using OOP. But of course you can convert it into OOP instead.

In each operation function, you'll find and infinite loop that check if both numerator and denominator can be divide by 2,3,5 or 7 by using remainder. The loop will go back and forth until one or both can't be divide anymore. (* you can add more prime number like 11 or 13 or any prime number.)

(* I used Code::Blocks the open source cross-platform IDE on windows 7)

So here is the sample output:

Addition:

Subtraction:


Multiplication:




















Division:


Popular posts from this blog

Extra : Play DOTA 2 Offline The Simplified Way

Create a Custom Blogger Header Part 2