Python Program to Find LCM


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co

LCM stands for Least Common Multiple, which is the smallest positive integer that is a multiple of two or more integers. In other words, it is the smallest number that can be divided by each of the given numbers without leaving a remainder.

To find the LCM of two or more numbers, you can start by listing their multiples until you find the smallest multiple that is common to all of them. For example, the LCM of 4 and 6 is 12, because 12 is the smallest multiple that both 4 and 6 have in common.


Python Code :

The below Python program finds the least common multiple (LCM) of two numbers:


# take input from user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# determine the greater number
if num1 > num2:
    greater = num1
else:
    greater = num2

# iterate from greater to the product of the two numbers and check for multiples
lcm = greater
while True:
    if lcm % num1 == 0 and lcm % num2 == 0:
        break
    lcm += greater

# print the LCM
print("The LCM of", num1, "and", num2, "is", lcm)

In this program, we first take input from the user for the two numbers whose LCM is to be found. We then determine the greater of the two numbers using an if statement.

We then use a while loop to iterate from the greater number to the product of the two numbers, checking for multiples of both numbers using the modulus operator (%). If a common multiple is found, we break out of the loop and store it in the variable lcm.

Finally, we print the LCM of the two numbers using the print() function. The output will be in the format of “The LCM of (num1) and (num2) is (lcm)”.

For example :


Enter first number: 25
Enter second number: 30
The LCM of 25 and 30 is 150


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co