Python Program to Find LCM

Created with Sketch.

Python Program to Find LCM

LCM: Least Common Multiple/ Lowest Common Multiple

LCM stands for Least Common Multiple. It is a concept of arithmetic and number system. The LCM of two integers a and b is denoted by LCM (a,b). It is the smallest positive integer that is divisible by both “a” and “b”.

For example: We have two integers 4 and 6. Let’s find LCM

Multiples of 4 are:

  1. 4812162024283236,… and so on…

Multiples of 6 are:

  1. 6121824303642,… and so on….

Common multiples of 4 and 6 are simply the numbers that are in both lists:

  1. 122436486072,…. and so on….

LCM is the lowest common multiplier so it is 12.

See this example:

  1. def lcm(x, y):
  2.    if x > y:
  3.        greater = x
  4.    else:
  5.        greater = y
  6.   while(True):
  7.        if((greater % x == 0and (greater % y == 0)):
  8.            lcm = greater
  9.            break
  10.        greater += 1
  11.    return lcm
  12. num1 = int(input(“Enter first number: “))
  13. num2 = int(input(“Enter second number: “))
  14. print(“The L.C.M. of”, num1,“and”, num2,“is”, lcm(num1, num2))

The following example will show the LCM of 12 and 20 (according to the user input)

Output:

Python Function Programs1

 

Leave a Reply

Your email address will not be published. Required fields are marked *