about-text

SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming languages. SoftOSet is the hub of the informative and research based articles.

Write Program in C++ to Calculate Fractions

So, our question is to write the code to calculate fractions in c++ programming language.  It’s right platform for every type of code and programs. Now let’s start our journey. We will write a program in the C++ programming language which will first of all inquire about the operation to be performed on fraction. When you will enter the operator (+,-,*, /,) it will ask you to enter the first fraction and you should have to enter the fraction in a/b manner then it will ask you to enter second fraction as x/y and then it will give you the result also in fractional form as f/g.


Question to Be Addressed

Create a four-function calculator for fractions.Here are the formulas for the four arithmetic operations applied to fractions:

Addition: a/b + c/d = (a*d + b*c) / (b*d)

Subtraction: a/b - c/d = (a*d - b*c) / (b*d)

Multiplication: a/b * c/d = (a*c) / (b*d)

Division: a/b / c/d = (a*d) / (b*c)

The user should type the first fraction, an operator, and a second fraction. The program

should then display the result and ask whether the user wants to continue.

Program to Make Fractional Calculator

To make a fractional calculator is not an easy task you must have to be thorough and attentive while understanding the code. Then you will able to learn how to make the fractional calculator.

One thing to be noted that these programs are compiled and tested on the Dev C++ IDE. So if you want the same output from them download and install the Dev C++ from here and then run the code.

Code

#include<iostream>

void add();                                        

void sub();                                                    

void mul();                                        

void div();

char ch;

using namespace std;

int main()

{

            do{

           

            cout<<"Enter the opertor to Perform (+,-,/,*)"<<endl;

            char choice;

           

            cin>>choice;

           

            switch(choice)

            {

                        case '+':

                                    add();

                                    break;

                                                            case '-':

                                    sub();

                                    break;

                                                            case '*':

                                    mul();

                                    break;

                                                            case '/':

                                    div();

                                    break;

            }

            cout<<"\n"<<"Want to Calculate Again (y/n)?"<<endl;

               

                                    cin>>ch;

                                   

}while(ch=='y');

 

            return 0;

}

 

void add()

{

             

            int num1,num2,num3,num4,a,b,c;

           

            char op;

           

            int lcm;

           

            cout<<"Enter the Fraction no1 to be Add"<<endl;

           

            cin>>num1>>op>>num2;

           

            cout<<"+";

           

                        cout<<"Enter the Fraction no2 to be Add"<<endl;

                       

            cin>>num3>>op>>num4;

           

            if(num2==num4)

            {

           

                  lcm=num4;

                       

            c=num1+num3;

           

            cout<<"Answer= ";

           

            cout<<c<<"/"<<lcm;

}

            else

            {

           

            lcm=num2*num4;

           

            a=num4*num1;

           

            b=num3*num2;

           

            c=a+b;

           

                        cout<<"Answer= ";

                       

            cout<<c<<"/"<<lcm;

    }

}

 

void sub()

{

            int num1,num2,num3,num4,a,b,c;

           

            char op;

           

            int lcm;

           

            cout<<"Enter the Fraction no1 to be Sub"<<endl;

           

            cin>>num1>>op>>num2;

           

            cout<<"-";

           

                        cout<<"Enter the Fraction no2 to be Sub"<<endl;

                       

            cin>>num3>>op>>num4;

            if(num2==num4)

            {

           

            lcm=num4;

           

            c=num1-num3;

           

                        cout<<"Answer= ";

                       

                cout<<c<<"/"<<lcm;

           

}

            else

            {

           

            lcm=num2*num4;

            a=num4*num1;

            b=num3*num2;

            c=a-b;

                        cout<<"Answer= ";

            cout<<c<<"/"<<lcm;

}

}

void mul()

{

            int num1,num2,num3,num4,a,b,c;

            char op;

            int lcm;

            cout<<"Enter the Fraction no1 to be Multiply"<<endl;

            cin>>num1>>op>>num2;

            cout<<"*";

                        cout<<"Enter the Fraction no2 to be Multiply"<<endl;

            cin>>num3>>op>>num4;

            lcm=num2*num4;

            c=num1*num3;

                        cout<<"Answer= ";

            cout<<c<<"/"<<lcm;

}

void div()

{

            int num1,num2,num3,num4,a,b,c;

            char op;

            int lcm;

            cout<<"Enter the Fraction no1 to be Divide"<<endl;

            cin>>num1>>op>>num2;

            cout<<"/";

                        cout<<"Enter the Fraction no2 to be Divide"<<endl;

            cin>>num3>>op>>num4;

            lcm=num1*num4;

            c=num2*num3;

                        cout<<"Answer= ";

            cout<<c<<"/"<<lcm;

}

Output

And here is the sample output of this code. If you have any error about the code then contacts us. We will surely help you to run your code and resolve your error.

 

 

 

 

 

Explanation

What I have done in this code is just simply that, I have included the header file of the <iostream>. If you are using any other compiler instead of Dev C++, then you must have to add some other headers according to the requirement of your compiler. The next statement is using namespace std; It is just used in the Dev C++, if you or using any other compiler then you probably no need to add this statement.

First of all we have declared the four functions and a global data type before main().

void add();                                        

void sub();                                                    

void mul();                                        

void div();

char ch;

 

main()

In main we have first inserted the do while loop to repeat the process of the getting operator again and again until user finish his calculation on calculator. This do while loop ends one line above the return statement.

Next when user will select an operator, we have used a switch statement which judges the operator and then routes the operator to its related function.

This is all we have done.

Then next crucial step is of making the functions to calculate the fractions.

 

add()

In add function for addition of the fractions. We have declare a variable(lcm) to calculate the LCM of the denominators of the fractions, and then multiply the nominator of the first fraction with the denominator of second fraction and nominator of second fraction with the denominator of first fraction and then adding their result. This result becomes the nominator of the answer and LCM becomes denominator.

And if both the denominators are same then just any one of them is LCM and just simply add  the nominators.

sub()

In sub function for subtraction of the fractions. We have declare a variable(lcm) to calculate the LCM of the denominators of the fractions, and then multiply the nominator of the first fraction with the denominator of second fraction and nominator of second fraction with the denominator of first fraction and then subtracting  their result. This result becomes the nominator of the answer and LCM becomes denominator.

And if both the denominators are same then just any one of them is LCM and just simply subtracts the nominators.

mul()

In mul function for multiplication of the fractions. We have declare a variable(lcm) to calculate the LCM of the denominators of the fractions which is denominator. The nominator of both fractions are multiplied and the result is the nominator of answer.

 

div()

In div function for division of the fractions. Just invert the one fraction and then perform all the step of the multiplication.

 

 

At the end I have returned the control back to the system using the

return 0;

Statement.

Conclusion

Now I conclude the whole story in a single sentence

“Functions, Global Variables and Do While Statement are the hearth of Programming.”

If you have any type of bug or error in your code feel free to take guidance from us. Our team will try its best to help you to remove your bug.

 

Thank you

 

 


Post a Comment

Feel Free to Comment Us

Previous Post Next Post

Recents