Python Program to Find LCM of Two Numbers

In this example, we will learn to write a Python program to find the Least Common Multiples (LCM) using the functions. To better understand this example, make sure you have knowledge of the following tutorials:-

The least common multiple (L.C.M.) of two numbers is the smallest positive integer that is perfectly divisible by the two given numbers. For example, the L.C.M. of 12 and 14 is 84.

Python Program to Make a Simple Calculator

def lcm(x, y):
    """This function takes two
    integers and returns the L.C.M."""

    # choose the greater number
    if x > y:
        greater = x
    else:
        greater = y

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

    return lcm_result


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

print("The L.C.M. of", num1, "and", num2, "is", lcm(num1, num2))

The output of the above program is:-

Enter the first number: 15
Enter second number: 25
The L.C.M. of 15 and 25 is 75
Program Explanation:-
Here lcm is calculated using a function which takes two arguments as input numbers. The greater number between two is found out and assigned to the greater variable. Then, an infinite loop is created using a while loop that checks for the perfect division of both numbers to a greater value. The value of greater is increased until the perfect divisible number is found out. The control is given back to calling the function once that number is found using the break statement.
SHARE Python Program to Find LCM of Two Numbers

You may also like...

Leave a Reply

Your email address will not be published.

Share