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.

Add Fractions in C++ Program

This C++ program will add two fractions. It will take first fraction from user and then take the second fraction from the user and then add them and display the result on the screen in the fractional form, not in the simplified form.


Question to Be Addressed

Write a program that encourages the user to enter two fractions, and then displays their

sum in fractional form. (You don’t need to reduce it to lowest terms.) The interaction

with the user might look like this:

Enter first fraction: 1/2

Enter second fraction: 2/5

Sum = 9/10

You can take advantage of the fact that the extraction operator (>>) can be chained to

read in more than one quantity at once:

cin >> a >> dummychar >> b;

 

Program to Add Fractions

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>

using namespace std;

int main()

{

            float a,b,c,d,sum,up,down;

            char dummychar;

            cout<<"Enter the Two Fractions To be Add\n";

            cout<<"\nEnter the First Fraction :";

            cin>>a>>dummychar>>b;

                        cout<<"\nEnter the Second Fraction :";

            cin>>c>>dummychar>>d;

            up=((a*d)+(b*c));

            down=b*d;

            cout<<"\nSum= "<<up<<"/"<<down<<endl;

return 0;

}

Output

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

 

 

 

 

 

Explanation

What I have do 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.

 

main()

We have declared the following float data type variables for the following purposes.

a       To store the nominator of the first fraction.

b       To store the denominator of the first fraction.

c     To store the nominator of the second fraction.

d      To store the denominator of the second fraction.

up    To store the nominator of the sum.

down   To store the denominator of the sum.

 

sum          To store the Final Sum of the Fractions

 We have taken the first fraction from user using the code.

            cout<<"\nEnter the First Fraction :";

            cin>>a>>dummychar>>b;

 

then we have taken the first fraction from user using the code.

            cout<<"\nEnter the second Fraction :";

            cin>>c>>dummychar>>d;

We have written the following code to store the nominator and denominator of the sum of the fractions.

            up=((a*d)+(b*c));

            down=b*d;

and finally print the sum using statement

            cout<<"\nSum= "<<up<<"/"<<down<<endl;

in a logical way.

 That’s it.

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

“Wise use of the operators leads towards the addition of fractions.”

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