Python function program to find the LCM of two numbers

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
  1. Create a function lcm
  2. Use the def keyword to define the function.
  3. Pass two parameter i.e. numbers to the function.
  4. Using an if-else statement check which number is greater.
  5. Use While loop.
  6. Inside the While loop check whether both numbers divide the greater number or not.
  7. If yes then break the loop otherwise, add 1 to the greater number and let the loop continue.
  8. At the end print the output.
  9. 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

access a function inside a function.

calculate the sum of numbers from 0 to 10.

Leave a Comment