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.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

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


  • GSaiCharan

    n1=int(input(“num1”))
    n2=int(input(“num2”))
    d1=int(input(“dnum1”))
    d2=int(input(“dnum2″))
    s=n1*d2+n2*d1
    u=d1*d2
    print(s,”/”,u)


  • Pavan

    from fractions import *
    def fraction_sum(a,b):
    print(Fraction(Fraction(a)+Fraction(b)))

    fraction_sum(‘1/3′,’3/9’)


  • mahesh

    def add_fraction(f1,f2):
    num1,den1 = f1
    num2,den2 = f2

    common_den = den1 * den2

    new_num1 = num1 * den2
    new_num2 = num2 * den1

    sum_num = new_num1 + new_num2

    return (sum_num,common_den)

    f1 = (1,3)
    f2 = (1,4)
    result = add_fraction(f1,f2)
    print(result)


  • D

    n1,d1=map(int,list(input(“enter numerator and denominator of 1st fraction:”).split(” “)))
    n2,d2=map(int,list(input(“enter numerator and denominator of 1st fraction:”).split(” “)))
    lcm=d1*d2
    num=(n1*d2)+(n2*d1)
    fraction=num/lcm
    print(“raw fraction”,num,’/’,lcm)

    def gcd(numerator,lcm):
    g=0
    for i in range(1,min(n1,n2)+1):
    if n1%i==0 and n2%i==0:
    g=i
    return g

    numerator=num/gcd(num,lcm)
    denominator=lcm/gcd(num,lcm)
    reqfraction=numerator/denominator
    print(‘required fraction is sum of ‘,n1,’/’,d1,’+’,n2,’/’,d2,’=’,numerator,’/’,denominator)


  • 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))