Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Program for LCM Of Two Numbers

Find the LCM of Two Numbers

In this Python Program find the LCM of Two Numbers which numbers are entered by the user. Basically the LCM of two numbers is the smallest number which can divide the both numbers equally. This is also called Least Common Divisor or LCD.

Let up suppose, we have two numbers, 60 and 72
LCM = 2 * 2 * 2 * 3 * 3 * 5
Result : 360 as the LCM of both Numbers.

Python program to find LCM of Two Numbers

Working:

Step 1. Start

Step 2. Take two user input and store into int type variable. such as num1 and num2.

Step 3. Define a function name as lcmfinder(num1, num2) with arguments num1 and num2.

Step 4. Now, check if num1 > num2, then store num2 value in larger variable.

Step 5. Else, Store num1 value in larger variable.

Step 6.  Run a while loop which is true.

Step 7. Inside the while loop, check larger % num1 == 0 and larger % num2 == 0. If both conditions are true then only assign larger variable value to lcm variable and break while loop.

Step 8. Otherwise, increase larger variable value by 1 with each iteration.

Step 9. Finally, Print the lcm variable value.

Stop 10. Stop

Python Program:

num1 = int(input("Enter first number:"))
num2 = int(input("Enter Second Number:"))


def lcmFinder(num1, num2):
    if num1 > num2:
        larger = num1
    else:
        larger = num2
    while True:
        if (larger % num1 == 0) and (larger % num2 == 0):
            lcm = larger
            break
        larger = larger + 1
    print("LCM of two given number:{}".format(lcm))


lcmFinder(num1, num2)


# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student) 

Output:

Enter first number:4
Enter Second Number:6
LCM of two given number:12