Thursday, 5 January 2017

If you have two fractions, a/b and c/d, their sum can be obtained from the formula a c a*d + b*c --- + --- = ----------- b d b*d For example, 1/4 plus 2/3 is 1 2 1*3 + 4*2 3 + 8 11 --- + --- = ----------- = ------- = ---- 4 3 4*3 12 12 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;

Leave a Comment
Object-Oriented Programming in C++ Fourth Edition By Robert Lafore Chapter-2 C++ Programming Basics -- Questions+Exercises

Question:

If you have two fractions, a/b and c/d, their sum can be obtained from the formula

            a      c      a*d + b*c
            --- + ---  =  -----------
            b      d         b*d 

For example, 1/4 plus 2/3 is

            1       2       1*3 + 4*2       3 + 8       11
            --- + ---  =  -----------  =  -------  =  ----
            4       3          4*3                12        12

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;


Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
       char dumychar;
       int a, b, c, d,result1,result2;
       cout << "Enter The 1st Fraction = ";
       cin >> a >> dumychar >> b;
       cout << "Enter The 2nd Fraction = ";
       cin >> c >> dumychar >> d;
       result1 = (a*d) + (b*c);
       result2 = b*d;
       cout << "Result == " << result1 << "/" << result2<<endl;

}

Output:
If you have two fractions, a/b and c/d, their sum can be obtained from the formula              a      c      a*d + b*c             --- + ---  =  -----------             b      d         b*d   For example, 1/4 plus 2/3 is              1       2       1*3 + 4*2       3 + 8       11             --- + ---  =  -----------  =  -------  =  ----             4       3          4*3                12        12  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;



Related Articles:



If You Enjoyed This, Take 5 Seconds To Share It