This is a Python Program to find the LCM of two numbers.
The program takes two numbers and prints the LCM of two numbers.
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.
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
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.
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.