Skip to content
Home » LCM of Two Numbers in Python

LCM of Two Numbers in Python

Learn about LCM of Two Numbers in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

LCM of two numbers in Python: The smallest positive number that is divisible by both a and b is the least or lowest common multiple (LCM) of two integers a and b.

LCM of Two Numbers in Python

In Python, this is a standard method for calculating the lcm of two numbers. While declaring the variables, we will take two numbers. Using an if-else statement and a while loop, now we write a Python program to calculate the lcm of two numbers.

Source code:

# Python program to find the LCM of the two numbers

# inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# choose the greater number
if (num1 > num2):
    greater = num1
else:
    greater = num2

while(True):
    # find the LCM
    if(greater % num1 == 0 and greater % num2 == 0):
        print('The LCM of',num1,'and',num2,'is',greater)
        break
    greater += 1

Output:

Enter first number: 12
Enter second number: 16
The LCM of 12 and 16 is 48

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Number of Digits in a Number in Python using Recursion