Print LCM of Two Integers in Python
Computations and programming when combined make a very deadly combination. As the ability to solve complex mathematical questions in itself is a great deal. But when one has to do the same thing by writing up a code, things get somewhat more complicated. Not to mention the language your coding in also determines whether it’s going to be easy to difficult. So today we’re going to write a program to print LCM of Two Integers in Python.
What’s The Approach?
- Let us consider a and b to be the two Integers to find LCM of.
- Firstly we’ll find the gcd of these two numbers, i.e, using a condition where if
abecomes zero we’ll return
belse we’ll recursively
return b % a, a
- Next, we’ll return multiplication of a with gcd of two numbers, and again multiply it with b. The returned value will be our LCM.
Also Read: Print Cube Root of A Number in Java
Python Program To Print LCM of Two Integers
a = 15, b = 20
LCM of 15 and 20 is 60
# Python program to find LCM of two numbers # Recursive function to return gcd of a and b def gcd(a,b): if a == 0: return b return gcd(b % a, a) # Function to return LCM of two numbers def lcm(a,b): return (a / gcd(a,b))* b # Driver program to test above function a = 15 b = 20 print('LCM of', a, 'and', b, 'is', lcm(a, b))