# 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.

Examples 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.

#include <stdio.h>
int main()
{
int a, b,c,d,x,y,i,gcd;
printf(“\nEnter the numerator for 1st number : “);
scanf(“%d”,&a);
printf(“\nEnter the denominator for 1st number : “);
scanf(“%d”,&b);
printf(“\nEnter the numerator for 2nd number : “);
scanf(“%d”,&c);
printf(“\nEnter the denominator for 2nd number : “);
scanf(“%d”,&d);
x=(a*d)+(b*c); //numerator
y=b*d; //denominator
// Trick part. Reduce it to the simplest form by using gcd.
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}
printf(“\nThe added fraction is %d/%d “,x/gcd,y/gcd);
printf(“\n”);
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int a, b,c,d,x,y,i,gcd;
cout << “\nEnter the numerator for 1st number : “;
cin >> a;
cout << “\nEnter the denominator for 1st number : “;
cin >> b;
cout << “\nEnter the numerator for 2nd number : “;
cin >> c;
cout << “\nEnter the denominator for 2nd number : “;
cin >> d;
x=(a*d)+(b*c); //numerator
y=b*d; //denominator
// Trick part. Reduce it to the simplest form by using gcd.
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}
cout << “\nThe added fraction is “<<x/gcd << “/”<<y/gcd;
cout << endl;
return 0;
}

import java.util.*;
public class Main
{
public static void main(String[] args) {
int a, b,c,d,x,y,i,gcd = 0;
Scanner sc = new Scanner(System.in);
System.out.println(“\nEnter the numerator for 1st number : “);
a = sc.nextInt();
System.out.println(“\nEnter the denominator for 1st number : “);
b = sc.nextInt();
System.out.println(“\nEnter the numerator for 2nd number : “);
c = sc.nextInt();
System.out.println(“\nEnter the denominator for 2nd number : “);
d = sc.nextInt();
x=(a*d)+(b*c); //numerator
y=b*d; //denominator
// Trick part. Reduce it to the simplest form by using gcd.
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}
System.out.println(“\nThe added fraction is “+ x/gcd + “/” + y/gcd);
System.out.println();
}
}

a = int(input(“Enter the numerator for 1st number : “))
b = int(input(“Enter the denominator for 1st number : “))
c = int(input(“Enter the numerator for 2nd number : “))
d = int(input(“Enter the denominator for 2nd number : “))
x=(a*d)+(b*c) #numerator
y=b*d #denominator
# Trick part. Reduce it to the simplest form by using gcd.
i = 1
while(i <= x and i <= y):
if(x % i == 0 and y % i == 0):
gcd = i
i = i + 1

print(“The added fraction is”, end = ‘ ‘)
print(int(x/gcd),end=”)
print(“/”,end=”)
print(int(y/gcd))

Complexity of adding two fractions: O(n)