Learn Math With Python

7. Learn Math With Python – Fractions

7.1 Representing Fractional Number

A fractional number F is represented in the form a/b where both a and b are integers and b ≠ 0.

a = 0
b = 0

#asking for input of value 
a = int(input("Enter numerator part of a fractional number "))
b = int(input("Enter denominator part of a fractional number "))

#printing the fractional number
if(b == 0): 
print('Invalid fractional Number')
else:
print('Entered Fractional Number is ', a, '/', b)

7.2 Addition of Two Fractional Numbers

There are two rules to add two fractional numbers:

  • When denominators are same then numerators are added
  • When denominators are not same then
    • Find LCM of the two denominators
    • Multiply numerator of first fractional number by (LCM/(Denominator of First Fractional Number) and multiply numerator of second fractional number by (LCM/(Denominator of Second Fractional Number)
    • Add the two new numerators
a1 = 0
b1 = 0
a2 = 0
b2 = 0
a = 0
b = 0
lcm = 0


#asking for input of two fractional numbers 
a1 = int(input("Enter numerator part of first fractional number "))
b1 = int(input("Enter denominator part of first fractional number "))
a2 = int(input("Enter numerator part of second fractional number "))
b2 = int(input("Enter denominator part of second fractional number "))

print('First Fractional Number is ', a1, '/', b1)
print('Second Fractional Number is ', a2, '/', b2)

#Adding Two Fractional Numbers
greater = 0

if(b1 == b2): #Checking whether denominators are same
  a = a1 + a2 
  b = b1
else:
  if b1 > b2: 
    greater = b1 
  elif b1 < b2: 
    greater = b2
  else:
    lcm = b1

while(True):
  if((greater % b1 == 0) and (greater % b2 == 0)):
    lcm = greater
    break
  greater += 1
if b1 == b2:
  lcm = b1
  
a1 = (lcm / b1) * a1 #Finding new numerator for first fractional number
a2 = (lcm / b2) * a2 #Finding new numerator for second fractional number

a = a1 + a2
b = lcm

print('Sum of ', int(a1), '/', int(b1), 'and', int(a2), '/', int(b2), 'is', int(a), '/', int(b))

7.3 Subtraction of Two Fractional Numbers

There are two rules to add two fractional numbers:

  • When denominators are same then numerators are added
  • When denominators are not same then
    • Find LCM of the two denominators
    • Multiply numerator of first fractional number by (LCM/(Denominator of First Fractional Number) and multiply numerator of second fractional number by (LCM/(Denominator of Second Fractional Number)
    • Subtract the two new numerators
a1 = 0
b1 = 0
a2 = 0
b2 = 0
a = 0
b = 0
lcm = 0

#asking for input of two fractional numbers 
a1 = int(input("Enter numerator part of first fractional number "))
b1 = int(input("Enter denominator part of first fractional number "))
a2 = int(input("Enter numerator part of second fractional number "))
b2 = int(input("Enter denominator part of second fractional number "))

print('First Fractional Number is ', a1, '/', b1)
print('Second Fractional Number is ', a2, '/', b2)

#Subtracting Two Fractional Numbers
greater = 0

if(b1 == b2): #Checking whether denominators are same
  a = a1 + a2 
  b = b1
else:
  if b1 > b2: 
    greater = b1 
  elif b1 < b2: 
    greater = b2
  else:
    lcm = b1

while(True):
  if((greater % b1 == 0) and (greater % b2 == 0)):
    lcm = greater
    break
  greater += 1
if b1 == b2:
  lcm = b1
  
a1 = (lcm / b1) * a1 #Finding new numerator for first fractional number
a2 = (lcm / b2) * a2 #Finding new numerator for second fractional number

a = a1 - a2
b = lcm

print('Difference is', int(a), '/', int(b))

7.4 Simplest Form of a Fractional Number

A fractional number is said to be in its simplest form, if numerator and denominator don’t have any common factor. 

For example ⅔, 11/15, 9/40 are in simplest form.

10/15 is not in its simplest form as both 10 and 15 are divisible by 5, i.e., HCF of 10 and 15 is 5. Hence, the simplest form of 

10/15 is obtained by dividing both 10 and 15 by 5.

Simplest form of 10/15 = ⅔ (10/5 = 2 and 15/5 = 3).

#Simplest Form of a Fractional Number
a1 = 0
b1 = 0
a2 = 0
b2 = 0

#asking for input of a fractional number 
a1 = int(input("Enter numerator part of fractional number "))
b1 = int(input("Enter denominator part of fractional number "))

smaller = 0
#Checking for Simplest Form
if a1 > b1:
smaller = b1
else:
smaller = a1

for i in range(1, smaller + 1):
if((a1 % i == 0) and (b1 % i == 0)):
hcf = i

if(hcf == 1):
print(a1, '/', b1,  'is in simplest form ')
else:
a2 = int(a1/hcf)
b2 = int(b1/hcf)
print('Simplest form of ', a1, '/', b1, 'is', a2, '/', b2)

7.5 Multiplication of Two Fractional Numbers

Product of two fractional numbers is obtained by multiplying the numerator of the first fractional number with the numerator of the second fractional number and multiplying the denominator of the first fractional number with the denominator of the second fractional number.

For example, (3/7) * (⅖) = (3 * 2)/(7 * 5) = 6/35 (which is in the simplest form)

(⅔) * (6/11) = (2 * 6)/(3 * 11) = 12/33

12/33 is not in its simplest form as 3 is a common factor. Its simplest form is 4/11

So, (⅔) * (6/11) = 4/11

#Multiplication of a Fractional Numbers
a1 = 0
b1 = 0
a2 = 0
b2 = 0
a = 0
b = 0

#asking for input of fractional numbers 
a1 = int(input("Enter numerator part of first fractional number "))
b1 = int(input("Enter denominator part of first fractional number "))
a2 = int(input("Enter numerator part of first fractional number "))
b2 = int(input("Enter denominator part of first fractional number "))
print('First Fractional Number is ', a1, '/', b1)
print('Second Fractional Number is ', a1, '/', b1) 

#Finding product of fractional numbers
a = a1 * a2
b = b1 * b2

#Checking for Simplest Form
if a > b:
smaller = b
else:
smaller = a
    
for i in range(1, smaller+1):
if((a % i == 0) and (b % i == 0)):
hcf = i

if(hcf == 1):
print('Product of ', a1, '/', b1, ' and ', a2, '/', b2, ' is ', int(a), '/', int(b))
else:
a = a/hcf
b = b/hcf
print('Product of ', a1, '/', b1, ' and ', a2, '/', b2, ' is ', int(a), '/', int(b))

7.6 Reciprocal of a Fractional Number

Reciprocal of a fractional number is obtained by interchanging the numerator and denominator of a fractional number.

For example reciprocal of 2/15 is 15/2

#Reciprocal of a Fractional Numbers
a = 0
b = 0

#asking for input of a fractional number
a = int(input("Enter numerator part of fractional number "))
b = int(input("Enter denominator part of fractional number "))

print('Reciprocal of ', a, '/', b, ' is ', b, '/', a)

7.7 Division of Two Fractional Numbers

Dividing a fractional number by another fractional number is the same as multiplying the first fractional number by the reciprocal of the second fractional number.

#Division of a Fractional Numbers
a1 = 0
b1 = 0
a2 = 0
b2 = 0
a = 0
b = 0

#asking for input of two fractional numbers
a1 = int(input("Enter numerator part of first fractional number "))
b1 = int(input("Enter denominator part of first fractional number "))
a2 = int(input("Enter numerator part of second fractional number "))
b2 = int(input("Enter denominator part of second fractional number "))

#Dividing two fractional numbers
a = a1 * b2
b = a2 * b1

#Checking for the Simplest Form of Result
if a > b:
smaller = b
else:
smaller = a
    
for i in range(1, smaller+1):
if((a % i == 0) and (b % i == 0)):
hcf = i

if(hcf == 1):
print('Division of ', a1, '/', b1, ' and ', a2, '/', b2, ' is ', int(a), '/', int(b))
else:
a = a/hcf
b = b/hcf
print('Division of ', a1, '/', b1, ' and ', a2, '/', b2, ' is ', int(a), '/', int(b))

Leave a Comment