Add two fractions | Program to add two fractions and display their sum in reduced form

Add two fractions | Program to add two fractions and display their sum in reduced form

A simple program to add two fractions and display their sum in the simplest form is discussed here. Let us assume the fractions to be a/b and c/d. Now we need to add these and find out the resultant fraction.


Add two fractions | Program to add two fractions and display their sum in reduced form

Examples of Test cases:

Input: 1 2 3 2

Output: 2 1

Input: 1 3 3 9

Output: 2 3

Input: 1 5 3 15

Output: 2 5


Algorithm to add two fractions

1) Calculate the numerator and denominator of the resultant fraction (x/y).

2) Find the gcd of x and y.

3) Divide x and y by their gcd to get the resultant fraction in the simplest form.


Add two fractions | Program to add two fractions and display their sum in reduced form

Program to add two fractions


@@coding::1@@



Complexity of adding two fractions: O(n)


Recommended Programs






Add two fractions | Program to add two fractions and display their sum in reduced form

c