Program to Find LCM of Two Numbers

28-Oct-2022

.

Admin

Program to Find LCM of Two Numbers

Hi Dev,

This article will give you example of Program to Find LCM of Two Numbers. Here you will learn Write a Python code to find the L.C.M. of two numbers. this example will help you LCM of Two Numbers in Python. if you want to see example of Python Least common multiple of two positive integers then you are a right place. Here, Creating a basic example of How to find LCM of 2 Numbers in Python With Code Examples.

Python Programs to Find LMC (least common multiple)

so let's see following examples with output:

Example 1: Python Program to find LCM of Two Numbers using while loop


# 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

Output:

Please Enter the First Value a: 12

Please Enter the Second Value b: 24

LCM of 12.0 and 24.0 = 84.0

Example 2: Python Program to find LCM of Two Numbers using Functions

# 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))

Output:

Please Enter the First Value a: 15

Please Enter the Second Value b: 20

LCM of 15.0 and 20.0 = 60.0

Example 3: Python Program to find LCM of Two Numbers using Functions

# 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))

Output:

Please Enter the First Value a: 15

Please Enter the Second Value b: 20

LCM of 15.0 and 20.0 = 60.0

Example 4: Progarm for LCM of Two numbers in Python using Recursion

# 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))

Output:

Please Enter the First Value a: 50

Please Enter the Second Value b: 80

LCM of 50.0 and 80.0 = 400.0

I hope it can help you...

#Python