# Python Program to find LCM of Two Numbers

Write a Python program to find the LCM of two numbers using While Loop, Functions, and Recursion.

In Mathematics, the Least Common Multiple of two or more integers is the smallest positive integer that is perfectly divisible by the given integer values without the remainder. For example, the LCM value of integers 2 and 3 is 12 because 12 is the smallest positive integer that is divisible by both 2 and 3 (the remainder is 0).

## Python Program to find LCM of Two Numbers Example 1

This python program allows the user to enter two positive integer values. Within the Python while loop, we used the If statement to check whether the remainder of maximum % a and maximum % b equals zero or not. If true, Least Common Multiple = maximum otherwise, skip that value.

```# using If Else and While loop
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 Least Common Multiple of {0} and {1} = {2}".format(a, b, maximum))
break;
maximum = maximum + 1```
`````` Please Enter the First Value a: 15
Please Enter the Second Value b: 20

Least Common Multiple of 15.0 and 20.0 = 60.0``````

## Python Program to find LCM of Two Numbers using Functions

This Python program is the same as above. However, we are separating the logic using the Functions.

```def findLeastCM(a, b):
if(a > b):
maximum = a
else:
maximum = b

while(True):
if(maximum % a == 0 and maximum % b == 0):
Val = maximum;
break;
maximum = maximum + 1
return Val

num1 = float(input(" Please Enter the First : "))
num2 = float(input(" Please Enter the Second : "))
Val = findLeastCM(num1, num2)
print("\n Least Common Multiple of {0} and {1} = {2}".format(num1, num2, Val))```
`````` Please Enter the First : 20
Please Enter the Second : 45

Least Common Multiple of 20.0 and 45.0 = 180.0``````

## LCM of Two Numbers using GCD

This program finds the GCD of two numbers. Here, we are using the Temp variable to find it. Using this, we calculate LCM.

```num1 = float(input(" Please Enter the First : "))
num2 = float(input(" Please Enter the Second : "))

a = num1
b = num2

while(num2 != 0):
temp = num2
num2 = num1 % num2
num1 = temp

hcf = num1
print("\n HCF of {0} and {1} = {2}".format(a, b, hcf))

Val = (a * b) / hcf
print("\n Result of {0} and {1} = {2}".format(a, b, Val))```
`````` Please Enter the First : 2
Please Enter the Second : 3

HCF of 2.0 and 3.0 = 1.0

Result of 2.0 and 3.0 = 6.0``````

## Using Recursion

This program allows the user to enter two positive integer values and calculate the LCM of those two values by calling fdhcf function recursively.

```def fdhcf(a, b):
if(b == 0):
return a;
else:
return fdhcf(b, a % b)

num1 = float(input(" Please Enter the First Value : "))
num2 = float(input(" Please Enter the Second Value : "))

gcd = fdhcf(num1, num2)
print("\n GCD of {0} and {1} = {2}".format(num1, num2, gcd))

res = (num1 * num2) / gcd
print("\n LCM of {0} and {1} = {2}".format(num1, num2, res))```