Adding Two Fractions and Displaying the Simplest Form
This article explains how to add two fractions and simplify the result into the lowest terms. This is a common problem in arithmetic, often solved using the Least Common Denominator (LCD) method and simplifying the result using the Greatest Common Divisor (GCD).
Algorithm to Add Two Fractions
Given two fractions ab\frac{a}{b}ba and cd\frac{c}{d}dc:
Calculate the numerator of the resulting fraction: x=(a×d)+(c×b)x = (a \times d) + (c \times b)x=(a×d)+(c×b)
Calculate the denominator of the resulting fraction: y=b×dy = b \times dy=b×d
Compute the GCD of xxx and yyy to simplify the fraction.
Divide both numerator and denominator by the GCD to get the simplest form.
Python Program
python
from math import gcddefadd_fractions(a, b, c, d):
# Calculate numerator and denominator
numerator = a * d + c * b
denominator = b * d# Simplify using GCD
common_divisor = gcd(numerator, denominator)
numerator //= common_divisor
denominator //= common_divisorreturn numerator, denominator# Input: Fractions a/b and c/d
a, b = map(int, input("Enter numerator and denominator of first fraction (a b): ").split())
c, d = map(int, input("Enter numerator and denominator of second fraction (c d): ").split())# Add fractions
result_numerator, result_denominator = add_fractions(a, b, c, d)# Output resultprint(f"The sum of the fractions is: {result_numerator}/{result_denominator}")
Examples
Input:
1 2 3 2
Output:
python
The sum of the fractions is: 2/1
Input:
1 3 3 9
Output:
python
The sum of the fractions is: 2/3
Explanation of Steps
Finding Numerator and Denominator: For 12\frac{1}{2}21 and 32\frac{3}{2}23:
This article explains how to add two fractions and simplify the result into the lowest terms. This is a common problem in arithmetic, often solved using the Least Common Denominator (LCD) method and simplifying the result using the Greatest Common Divisor (GCD).
Algorithm to Add Two Fractions
Given two fractions ab\frac{a}{b}ba and cd\frac{c}{d}dc:
Calculate the numerator of the resulting fraction: x=(a×d)+(c×b)x = (a \times d) + (c \times b)x=(a×d)+(c×b)
Calculate the denominator of the resulting fraction: y=b×dy = b \times dy=b×d
Compute the GCD of xxx and yyy to simplify the fraction.
Divide both numerator and denominator by the GCD to get the simplest form.
Python Program
python
from math import gcddefadd_fractions(a, b, c, d):
# Calculate numerator and denominator
numerator = a * d + c * b
denominator = b * d# Simplify using GCD
common_divisor = gcd(numerator, denominator)
numerator //= common_divisor
denominator //= common_divisorreturn numerator, denominator# Input: Fractions a/b and c/d
a, b = map(int, input("Enter numerator and denominator of first fraction (a b): ").split())
c, d = map(int, input("Enter numerator and denominator of second fraction (c d): ").split())# Add fractions
result_numerator, result_denominator = add_fractions(a, b, c, d)# Output resultprint(f"The sum of the fractions is: {result_numerator}/{result_denominator}")
Examples
Input:
1 2 3 2
Output:
python
The sum of the fractions is: 2/1
Input:
1 3 3 9
Output:
python
The sum of the fractions is: 2/3
Explanation of Steps
Finding Numerator and Denominator: For 12\frac{1}{2}21 and 32\frac{3}{2}23: