Learn about LCM of Two Numbers using Function in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
LCM of Two Numbers using Function in Python
In the below program, the function find_lcm() takes two numbers as arguments and returns the lcm of the both numbers.
Source code:
# Python program to find the LCM using function
#user-defined function
def find_lcm(a, b):
if a > b:
greater = a
else:
greater = b
while(True):
# LCM
if((greater % a == 0) and (greater % b == 0)):
lcm = greater
break
greater += 1
return lcm
# inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
# function call
print('The LCM of',num1,'and',num2,'is',find_lcm(num1, num2))
Output:
Enter first number: 4
Enter second number: 6
The LCM of 4 and 6 is 12
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : LCM of Two Numbers in Python