Program to Find LCM in Python

Program to Find LCM in Python. The program is having while loop, if else loop, input and print functions. This is for beginners, class 11,12 and B Tech Computer science students.

Program to Find LCM in Python

Lowest common multiple / Least common multiple

LCM stands for Least Common Multiple. It is a concept of arithmetic and number system. The LCM of two integers a and b is denoted by LCM (a,b). It is the smallest positive integer that is divisible by both “a” and “b”.

Example- LCM of 4 and 6
Multiples of 4 are:
4, 8, 12, 16, 20, 24, 28, 32, 36,… and so on…  
Multiples of 6 are:
6, 12, 18, 24, 30, 36, 42,… and so on….  
Common multiples of 4 and 6 are simply the numbers that are in both lists:
12, 24, 36, 48, 60, 72,…. and so on….  
LCM is the lowest common multiplier so it is 12.
Since, we have understood the basic concept of the LCM, let us consider the following program to find the LCM of given integers.

Source code : Program to Find LCM in Python

# defining a function to calculate LCM  
def calculate_lcm(x, y):  
    # selecting 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    
  
# taking input from users  
num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))  
# printing the result for the users  
print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2)) 

Download Source code : For Download Click on the below Button

Source code explanation:

Step 1: Function calculate_lcm(x,y) : is defined. If condition is applied and while loop is applied.
def calculate_lcm(x, y):
# selecting 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
Step 2 : Input is taken.
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
Step 3 :LCM function is calculated in print statement:
print(“The L.C.M. of”, num1,”and”, num2,”is”, calculate_lcm(num1, num2))

Output :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Enter first number: 25
Enter second number: 12
The L.C.M. of 25 and 12 is 300
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Enter first number: 4
Enter second number: 6
The L.C.M. of 4 and 6 is 12
>>>

Output Explanation:

The two numbers whose LCM is to be calculated is feed.
Enter first number: 25
Enter second number: 12

LCM of numbers is calculated and printed.
The L.C.M. of 25 and 12 is 300

Conclusion:

The program is running fine.

Thank you for reading and learning from Program to Find LCM in Python post. The program is having while loop, if else loop, input and print functions. This is for beginners, class 11,12 and B Tech Computer science students. For more Python program click link below-
Python Programs Archives – CS Study

Leave a Comment

Your email address will not be published. Required fields are marked *