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.

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) 

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

 

Addition of two fractions in python

Algorithm

  • Step 1:- Start.
  • Step 2:- Take user inputs for both fractions.
  • Step 3:- Check if both the denominators are same.
  • Step 4:- If step 3 is TRUE add numerators of both the fractions.
  • Step 5:- Print the output in format of fraction.
  • Step 6:- If step 3 is FALSE apply cross multiplication on numerator.
  • Step 7:- Print it in format of fraction.
  • Step 8:- End.
Addition of two fractions

Python program for addition of two fractions

#take inputs
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
    Fraction = f1_nume + f2_nume
    #print
    print('Addition of two fractions are :' + str(Fraction) + '/' + str(f1_deno))
#if denominators are not same    
else:
    #to find the sum
    #denominators should be same
    #apply cross Multiplication
    Fraction = (f1_nume * f2_deno) + (f2_nume * f1_deno)
    print('Addition of fractions are :' + str(Fraction) + '/' + str(f1_deno * f2_deno))
Output:
Enter the numerator for 1st fraction :14
Enter the denominator for thr 1st fraction :10
Enter the numerator for 2nd fraction :24
Enter the denominator for the 2nd fraction :3
Addition of fractions are :282/30