Placement Prep

Add Two Fractions and Display Simplest Form: Python Guide

Python program to add two fractions and reduce to simplest form using math.gcd. Algorithm, code, three verified examples, and edge cases for placement coding rounds.

By FACE Prep Team 4 min read
python fractions gcd placement-prep coding-rounds arithmetic

Adding two fractions in Python takes four steps: cross-multiply to find a common numerator and denominator, compute the GCD of those two values, then divide both by it.

The Python math.gcd function handles the reduction in one call. This pattern appears in AMCAT coding sections and in-house placement tests at IT companies. Worth knowing cold before a coding round.

The Four-Step Algorithm

Given two fractions a/b and c/d, the algorithm works as follows:

  • Step 1: Compute the numerator of the sum: numerator = (a * d) + (c * b)
  • Step 2: Compute the denominator of the sum: denominator = b * d
  • Step 3: Find the GCD of numerator and denominator.
  • Step 4: Divide both by the GCD to get the simplest form.

Cross-multiplying at Steps 1 and 2 always gives a valid common denominator, even when b and d already share a factor. The GCD reduction at Steps 3 and 4 collapses the result correctly whether it ends up as a proper fraction, an improper fraction, or a whole number.

The algorithm is the standard cross-multiplication method taught in school arithmetic. Python’s math.gcd replaces the manual trial-division step you’d do by hand.

Python Implementation

from math import gcd

def add_fractions(a, b, c, d):
    """Return the simplest form of (a/b) + (c/d) as a (numerator, denominator) tuple."""
    numerator = (a * d) + (c * b)
    denominator = b * d
    common = gcd(abs(numerator), abs(denominator))
    return numerator // common, denominator // common

# Read input
a, b = map(int, input("First fraction — numerator denominator: ").split())
c, d = map(int, input("Second fraction — numerator denominator: ").split())

num, den = add_fractions(a, b, c, d)
print(f"{num}/{den}")

Two details worth noting. First, abs() is applied to both arguments of gcd so the function works correctly in Python 3.8 and below, where the math module’s gcd does not accept negative inputs. Second, the // operator does integer floor division, which is safe here because both numerator and denominator are already divisible by common.

Time and space complexity of the full operation:

  • Time: O(log(min(a, b))) for the GCD computation (Euclidean algorithm). The addition and division steps are O(1).
  • Space: O(1). No auxiliary data structures.

Three Worked Examples

Each example below was re-derived from first principles.

Example 1: 1/2 + 3/2

  • Step 1: numerator = (1 x 2) + (3 x 2) = 2 + 6 = 8
  • Step 2: denominator = 2 x 2 = 4
  • Step 3: GCD(8, 4) = 4
  • Step 4: 8 / 4 = 2, 4 / 4 = 1
  • Result: 2/1

Example 2: 1/3 + 3/9

  • Step 1: numerator = (1 x 9) + (3 x 3) = 9 + 9 = 18
  • Step 2: denominator = 3 x 9 = 27
  • Step 3: GCD(18, 27) = 9
  • Step 4: 18 / 9 = 2, 27 / 9 = 3
  • Result: 2/3

Example 3: 3/4 + 1/6

  • Step 1: numerator = (3 x 6) + (1 x 4) = 18 + 4 = 22
  • Step 2: denominator = 4 x 6 = 24
  • Step 3: GCD(22, 24) = 2
  • Step 4: 22 / 2 = 11, 24 / 2 = 12
  • Result: 11/12

Cross-check: 3/4 + 1/6 via LCD is 9/12 + 2/12 = 11/12. Both approaches agree.

Edge Cases to Know

Placement tests occasionally probe boundary conditions. Three come up repeatedly.

Same Denominators

For 2/5 + 3/5, cross-multiplication still works: numerator = (2 x 5) + (3 x 5) = 25, denominator = 5 x 5 = 25, GCD(25, 25) = 25, result: 1/1. The code handles this without any special case. If a cleaner display is required, add if den == 1: print(num) after the calculation.

Result Already in Simplest Form

For 1/4 + 1/4: numerator = (1 x 4) + (1 x 4) = 8, denominator = 4 x 4 = 16, GCD(8, 16) = 8, result: 1/2. The GCD step reduces correctly when the cross-multiplied values already share a large factor.

Negative Fractions

Python 3.9 updated math.gcd to accept negative integers. On Python 3.9+, gcd(-6, 9) returns 3. On Python 3.8 and below, passing a negative value raises a ValueError. The abs() wrapper in the implementation above keeps the function portable across both versions.

Using Python’s fractions Module

For quick scripts, the Python fractions module provides a Fraction class that reduces automatically:

from fractions import Fraction

a, b, c, d = 3, 4, 1, 6
result = Fraction(a, b) + Fraction(c, d)
print(result)  # 11/12

Fraction handles reduction, negative inputs, and display formatting out of the box. The manual math.gcd approach above is what placement coding questions ask you to implement. The question is usually framed as “write a function” rather than “use a library.” Know both; reach for Fraction in production, reach for the manual method in an assessment.

Fraction Arithmetic in Placement Coding Rounds

The add_fractions function above is a clean example of a pattern AMCAT coding sections use repeatedly: take an arithmetic operation, require reduction to simplest form, and test whether the candidate handles the GCD step. The same four-step logic transfers directly to fraction subtraction, multiplication simplification, and ratio reduction problems.

If you’re preparing for a placement coding section, the Python calculator program shows how arithmetic operations chain in a single interactive program, and the array sum program covers the accumulation pattern that many easy-tier problems extend. For more foundational programs at this level, the Python basic programs collection covers the full set before a coding round.

The four-step algorithm above, the edge cases at the same-denominator and negative-input boundaries, and the GCD reduction logic are all variations on the same problem class. TinkerLLM (₹299) gives you a browser-based Python environment to run the add_fractions function, test the edge cases from this article, and build the pattern recognition that placement coding tests reward.

Primary sources

Frequently asked questions

What is the simplest form of a fraction?

A fraction a/b is in simplest form when GCD(a, b) equals 1 — no integer other than 1 divides both numerator and denominator evenly.

Why cross-multiply denominators when adding fractions?

To express both fractions over a common denominator, you need a value that both b and d divide into evenly. The product b times d always works, though it may not be the least common denominator.

What does math.gcd do in Python?

math.gcd(a, b) returns the largest integer that divides both a and b exactly. Dividing both numerator and denominator by this value reduces the fraction to its simplest form.

What if the result is a whole number after adding two fractions?

The denominator becomes 1 after GCD reduction, so the output is n/1. You can optionally check if the denominator equals 1 and print just the numerator for a cleaner display.

Does this code handle negative fractions?

In Python 3.9 and above, math.gcd handles negative inputs correctly. For Python 3.8 and below, pass abs(numerator) to math.gcd and carry the sign separately in your return value.

Can I use Python's fractions.Fraction class instead of implementing math.gcd manually?

Yes. fractions.Fraction(a, b) + fractions.Fraction(c, d) returns a reduced Fraction object automatically. The manual math.gcd approach is what placement coding questions typically ask you to implement from scratch.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF