Python Program for Addition of two fractions

Addition of two fractions

Here, in this page we will discuss program for addition of two fractions in python. For this purpose we need to ask the user to enter two fractional values where each fraction must consist a Numerator and a Denominator.

Addition of two fractions in python

Concept

 For understanding this in a better way lets suppose an example :

 Suppose, First fraction consist of 1 as numerator and 3 as denominator, and Second fraction consist of 3 as numerator and 9 as denominator.

 (1 / 3) + (3 / 9) = (6 / 9) = (2 / 3)

  • Find LCM of 3 and 9 and the result will be 9.
  • Multiply 3 in first fraction : (3 / 9) + (3 / 9)
  • Add both fractions and then the result will be : (6 / 9)
  • Now simplify it by finding the HCF of 6 and 9 and the result will be 3.
  • So divide 6 and 9 by 3 and then the result will be : (2 / 3)
  • This will be your simplified answer for the given problem.

Algorithm

  • Initialize variables of numerator and denominator
  • Take user input of two fraction
  • Find numerator using this condition (n1*d2) +(d1*n2 ) where n1,n2 are numerator and d1 and d2 are denominator
  • Find denominator using this condition (d1*d2) for lcm
  • Calculate GCD of a this new numerator and denominator
  • Display a two value of this condition x / gcd, y/gcd
Addition of two fractions

Python code

Run
def findGCD(n1, n2):
    gcd = 0
    for i in range(1, int(min(n1, n2)) + 1):
        if n1 % i == 0 and n2 % i == 0:
            gcd = i
    return gcd


# input first fraction
num1, den1 = map(int, list(input("Enter numerator and denominator of first number : ").split(" ")))

# input first fraction
num2, den2 = map(int, list(input("Enter numerator and denominator of second number: ").split(" ")))

lcm = (den1 * den2) // findGCD(den1, den2)

sum = (num1 * lcm // den1) + (num2 * lcm // den2)

num3 = sum // findGCD(sum, lcm)

lcm = lcm // findGCD(sum, lcm)

print(num1, "/", den1, " + ", num2, "/", den2, " = ", num3, "/", lcm)

Output

Enter numerator and denominator of first number : 14 10
Enter numerator and denominator of second number: 24 3
14 / 10 + 24 / 3 = 47 / 5

For similar Questions click on the given button.

5 comments on “Python Program for Addition of two fractions”


  • Ganesh

    g1nume = int(input(‘Enter the numerator for 1st fraction :’))
    g1deno = int(input(‘Enter the denominator for the 1st fraction :’))
    g2nume = int(input(‘Enter the numerator for 2nd fraction :’))
    g2deno = int(input(‘Enter the denominator for the 2nd fraction :’))

    n = g1deno*g2deno
    o = g2deno*g1nume
    h = g1deno*g2nume
    k = o + h

    print(k, ‘/’, n)


  • Ganesh

    #Easy to understand
    g1nume = int(input(‘Enter the numerator for 1st fraction :’))
    g1deno = int(input(‘Enter the denominator for the 1st fraction :’))
    g2nume = int(input(‘Enter the numerator for 2nd fraction :’))
    g2deno = int(input(‘Enter the denominator for the 2nd fraction :’))

    n = g1deno*g2deno
    o = g2deno*g1nume
    h = g1deno*g2nume
    k = o + h

    print(k, ‘/’, n)


  • Gorgulls

    For same denominator , The above code is not fully combined:;

    f1_nume = int(input(‘Enter the numerator for 1st fraction :’))
    f1_deno = int(input(‘Enter the denominator for the 1st fraction :’))
    f2_nume = int(input(‘Enter the numerator for 2nd fraction :’))
    f2_deno = int(input(‘Enter the denominator for the 2nd fraction :’))
    #check if denominators are same
    if(f1_deno == f2_deno):
    #add numerator
    Fraction1 = f1_nume + f2_nume
    #print
    print(‘Addition of two fractions are :’,Fraction1 ,”/”,(f1_deno))
    #if denominators are not same
    else:
    #to find the sum
    #denominators should be same
    #apply cross Multiplication
    Fraction2 = (f1_nume * f2_deno) + (f2_nume * f1_deno)
    print(‘Addition of fractions are :’,(Fraction2),”/”, (f1_deno * f2_deno))


  • KIRA

    Can it Be done like this cause this is more easy:
    nume1 = int(input(“Enter Number : “))
    deno1 = int(input(“Enter Number : “))
    nume2 = int(input(“Enter Number : “))
    deno2 = int(input(“Enter Number : “))

    x = (nume1*deno2) + (nume2*deno1)
    y = deno1 * deno2
    print(“{}/{}”.format(x, y))