Easy Tutorial
❮ Python Your Font Python Area Of A Circle ❯

Python Least Common Multiple Algorithm

Python3 Examples

The following code is used to implement the Least Common Multiple algorithm:

Example (Python 3.0+)

# Filename : test.py
# author by : www.tutorialpro.org

# Define function
def lcm(x, y):

   # Get the largest number
   if x > y:
       greater = x
   else:
       greater = y

   while(True):
       if((greater % x == 0) and (greater % y == 0)):
           lcm = greater
           break
       greater += 1

   return lcm


# Get user input
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

print( num1,"and", num2,"have a least common multiple of", lcm(num1, num2))

Executing the above code produces the following output:

Enter the first number: 54
Enter the second number: 24
54 and 24 have a least common multiple of 216

Python3 Examples

❮ Python Your Font Python Area Of A Circle ❯