Program to find lcm of two numbers in python; Through this tutorial, i am going to show you how to find lcm of two numbers in python using recursion, for loop, and while loop.
LCM stands for Least common multiple. LCM is the method to find the smallest possible multiple of two or more numbers. LCM of two numbers is divisible by both the numbers. For example, LCM of 6 and 8 is 24. Hence 24 is divisible by both 6 and 8.
Python Program to find LCM of Two Numbers
- Python Program to find LCM of Two Numbers using while loop
- Python Program to find LCM of Two Numbers using Functions
- Progarm for LCM of Two numbers in Python using Recursion
Python Program to find LCM of Two Numbers using while loop
Use the below given steps and write a program to find lcm of two numbers in python using while loop:
- Take 2 input number from user
- Using if condition; find greater number
- Iterate while loop and find lcm of two number
- Print Lcm
# Python Program to find LCM of Two Numbers a = float(input(" Please Enter the First Value a: ")) b = float(input(" Please Enter the Second Value b: ")) if(a > b): maximum = a else: maximum = b while(True): if(maximum % a == 0 and maximum % b == 0): print("\n LCM of {0} and {1} = {2}".format(a, b, maximum)) break; maximum = maximum + 1
After executing the python program, the output will be:
Please Enter the First Value a: 25 Please Enter the Second Value b: 50 LCM of 25.0 and 50.0 = 50.0
Python Program to find LCM of Two Numbers using Functions
Use the below given steps and write a program to find lcm of two numbers in python using the function :
- Take 2 input number from user
- Using if condition; find greater number
- Create function and Call it with numbers.
- Print Lcm
# Python Program to find LCM of Two Numbers def findlcm(a, b): if(a > b): maximum = a else: maximum = b while(True): if(maximum % a == 0 and maximum % b == 0): lcm = maximum; break; maximum = maximum + 1 return lcm num1 = float(input(" Please Enter the First Value Num1 : ")) num2 = float(input(" Please Enter the Second Value Num2 : ")) lcm = findlcm(num1, num2) print("\n LCM of {0} and {1} = {2}".format(num1, num2, lcm))
After executing the python program, the output will be:
Please Enter the First Value a: 15 Please Enter the Second Value b: 20 LCM of 15.0 and 20.0 = 60.0
Progarm for LCM of Two numbers in Python using Recursion
Use the below given steps and write a program to find lcm of two numbers in python using recursion:
- Take 2 input number from user
- Using if condition; find greater number
- Calculate the GCD of those two values by calling findgcd function recursively
- Print Lcm
# Python Program to find LCM of Two Numbers def findgcd(a, b): if(b == 0): return a; else: return findgcd(b, a % b) num1 = float(input(" Please Enter the First Value Num1 : ")) num2 = float(input(" Please Enter the Second Value Num2 : ")) gcd = findgcd(num1, num2) print("\n GCD of {0} and {1} = {2}".format(num1, num2, gcd)) lcm = (num1 * num2) / gcd print("\n LCM of {0} and {1} = {2}".format(num1, num2, lcm))
After executing the python program, the output will be:
Please Enter the First Value a: 50 Please Enter the Second Value b: 80 LCM of 50.0 and 80.0 = 400.0