9. Program that encourages the user to enter two fractions, and then displays their sum in fractional form.


Chapter 2: 

C++ Programming Basics 

Programming Exercise

Problem # 9:

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;

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int a,b,c,d,resN,resD;
    char dummyChar;
    cout<<"Enter first fraction in the form of a/b:";
    cin>>a>>dummyChar>>b;
    cout<<"Enter second fraction in the form of c/d:";
    cin>>c>>dummyChar>>d;
    resN=(a*d)+(b*c);
    resD=b*d;
    cout<<"Sum is: "<<resN<<"/"<<resD;
   
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
8. Rewrite the WIDTH program so that the characters on each line between the location name and the population number are filled in with periods instead of spaces.

Comments