Python Program to Find LCM of Two Numbers

Python Program to Find LCM of Two Numbers

Program to find lcm of two numbers in python; In this tutorial, you will learn how to find lcm of two numbers in python using for loop, while loop, and recursion function.

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 numbers. For example, the LCM of 6 and 8 is 24. Hence 24 is divisible by both 6 and 8.

Algorithm for LCM of two numbers in Python

  • Take Inputs from the user using int(input()).
  • Find the greater number by using an If condition and assign it to the variable ‘max’.
  • Within the while loop, for loop or recursive function, Use an If condition to check whether the remainder of (max% a) and (max% b) equals to zero or not.
  • If true, Print max which is the LCM of 2 numbers,
  • Otherwise, skip that value using a break statement.
  • End of the program

Python Programs to Find LMC (least common multiple)

  • 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

Follow the below 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

Follow the below 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

Follow the below 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

Recommended Python Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *