Fraction Addition

please i want to know how can i add 2 numbers with fraction like this
3/4 + 5/3
how can i make this on C++ ??????????
Think how you would do that with math and translate that on C++
i cant understand what u want ???
Try this:

int main()
{
cout << "Insert upper and lower part of the fraction: ";
int a,b;
cin >> a >> b;
cout << "\n";
cout << "Insert upper and lower part of the fraction: ";
int c,d;
cin >> c >> d;
int e,f;
e = a*d+b*c;
f = b*d;
cout << "a/b + c/d = " << e << "/" << f;
cout << "\n";
return 0;
}
I think what Bazzy wanted to say is that it depends on the format of the answer that you want. Do you want your answer to be displayed as fraction? Or, you want your answer to be displayed with decimal point number.
Given fractions a/b and c/d,

Find the least common multiple of b and d. LCM algorithms are available on Wikipedia.

multiply a by LCM/b. multiply c by LCM/d.

Add the resulting numerators.

Reduce.
if the result is 6/4
How can i reduce it to 3/2
????????
By the code of C++
Last edited on
To reduce it, you need to determine the greatest common divisor. That can also be googled or wiki'd.
Given the fraction n/d, the reduced fraction is

(n/GCD(n,d)) over (d/GCD(n,d))

You could go to the library or the nearest book shop and get "C++ Without Fear" by Brian Overland. In this book you'll find how to make it and why it works.

Topic archived. No new replies allowed.