In this python function program, we will find the LCM of two numbers.
LCM:
LCM stands for ‘Least Common Multiple’ or the Lowest Common Multiple. The least common multiple (LCM) of two numbers is the lowest possible number that can be divisible by both numbers.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function lcm
- Use the def keyword to define the function.
- Pass two parameter i.e. numbers to the function.
- Using an if-else statement check which number is greater.
- Use While loop.
- Inside the While loop check whether both numbers divide the greater number or not.
- If yes then break the loop otherwise, add 1 to the greater number and let the loop continue.
- At the end print the output.
- Take two numbers as input through the user and pass them to the function while calling the function.
def lcm(num1,num2):
if num1 > num2:
greater = num1
else:
greater = num2
while(True):
if((greater % num1 == 0) and (greater % num2 == 0)):
lcm = greater
break
greater += 1
print(f"L.C.M of {num1} and {num2}: {lcm}")
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
lcm(num1,num2)
Output :
Enter number 1: 12
Enter number 2: 20
L.C.M of 12 and 20: 60
Related Articles
Python function program to calculate the sum of numbers from 0 to 10.
Python function program to find the HCF of two numbers.
Python function program to create a function with *args as parameters.
Python function program to get the factorial of a given number.
Python function program to get the Fibonacci series up to the given number.