Find LCM Using Loops

Find LCM of two numbers using loops.

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 5 of 13
find-lcm-using-loops.py
1# Take two numbers
2a = int(input("Enter first number: "))
3b = int(input("Enter second number: "))
4
5# Find LCM
6larger = max(a, b)
7lcm = larger
8
9while True:
10 if lcm % a == 0 and lcm % b == 0:
11 break
12 lcm += larger
13
14print(f"LCM of {a} and {b} is {lcm}")

Output

Enter first number: 12
Enter second number: 18
LCM of 12 and 18 is 36

What's going on

Find smallest number divisible by both.

Key Concepts:

Start from larger number
Check multiples of larger number
First number divisible by both is LCM