In this program, you'll learn to find the LCM of two numbers and display it.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python while Loop
- Python Functions
- Python Function Arguments
- Python User-defined Functions
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.
Program to Compute LCM
# Python Program to find the L.C.M. of two input number
def compute_lcm[x, y]:
# choose the greater 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
num1 = 54
num2 = 24
print["The L.C.M. is", compute_lcm[num1, num2]]
Output
The L.C.M. is 216
Note: To test this program, change the values of num1
and num2
.
This program stores two number in num1
and num2
respectively. These numbers are passed to the compute_lcm[]
function. The function returns the L.C.M of two
numbers.
In the function, we first determine the greater of the two numbers since the L.C.M. can only be greater than or equal to the largest number. We then use an infinite while
loop to go from that number and beyond.
In each iteration, we check if both the numbers perfectly divide our number. If so, we store the number as L.C.M. and break from the loop. Otherwise, the number is incremented by 1 and the loop continues.
The above program is slower to run. We can make it more efficient by using the fact that the product of two numbers is equal to the product of the least common multiple and greatest common divisor of those two numbers.
Number1 * Number2 = L.C.M. * G.C.D.
Here is a Python program to implement this.
Program to Compute LCM Using GCD
# Python program to find the L.C.M. of two input number
# This function computes GCD
def compute_gcd[x, y]:
while[y]:
x, y = y, x % y
return x
# This function computes LCM
def compute_lcm[x, y]:
lcm = [x*y]//compute_gcd[x,y]
return lcm
num1 = 54
num2 = 24
print["The L.C.M. is", compute_lcm[num1, num2]]
The output of this program is the same as before. We have two functions compute_gcd[]
and compute_lcm[]
. We require G.C.D. of the numbers to calculate its L.C.M.
So, compute_lcm[]
calls the function compute_gcd[]
to accomplish this.
G.C.D. of two numbers can be calculated efficiently using the Euclidean algorithm.
Click here to learn more about methods to calculate G.C.D in Python.
This is a Python Program to find the LCM of two numbers. Problem Description
The program takes two numbers and prints the LCM of two numbers.
Problem Solution
1. Take in both the integers and store it in separate variables.
2. Find which of the integer among the two is smaller and store it in a separate variable.
3. Use a while loop
whose condition is always True until break is used.
4. Use an if statement to check if both the numbers are divisible by the minimum number and increment otherwise.
5. Print the final LCM.
6. Exit.
Program/Source Code
Here is source code of the Python Program to find the LCM of two numbers. The program output is also shown below.
a=int[input["Enter the first number:"]] b=int[input["Enter the second number:"]] if[a>b]: min1=a else: min1=b while[1]: if[min1%a==0 and min1%b==0]: print["LCM is:",min1] break min1=min1+1
Program Explanation
1. User must enter both the numbers and store it in separate variables.
2. An if statement
is used to find out which of the numbers is smaller and store in a minimum variable.
3. Then a while loop is used whose condition is always true [or 1] unless break is used.
4. Then an if statement within the loop is used to check whether the value in the minimum variable is divisible by both the numbers.
5. If it is divisible, break statement breaks out of the loop.
6. If it is not divisible, the value in the minimum variable is incremented.
7. The final LCM is printed.
Runtime Test Cases
Case 1: Enter the first number:5 Enter the second number:3 LCM is: 15 Case 2: Enter the first number:15 Enter the second number:20 LCM is: 60
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.