LCM of two numbers in Python – Understand the logic

Just like GCD, I’m very much sure you have learned LCM in your school life too.

let us discuss the definition of the LCM.

The Least Common Multiple (LCM) is also referred to as the Lowest Common Multiple (LCM) and Least Common Divisor (LCD).

LCM of two numbers is the smallest common multiple or a positive integer which is divisible completely by both the numbers.

LCM is the least common multiple between two or more numbers which is wholly divisible by them.

LCM by Listing Multiples

  • List the multiples of each number until at least one of the multiples appears on all lists.
  • Find the smallest number that is on all of the lists.
  • This number is the LCM.

Multiples of 6: 6, 12, 18, 24, 30, 36, 42, 48, 54, 60

Multiples of 7: 7, 14, 21, 28, 35, 42, 56, 63, 70

Find the smallest number that is on all of the lists. We have it in bold above.

So LCM(6, 7) is 42

We want you to know about the LCM of two numbers, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

You still face difficulties in understanding the logic? That’s perfectly fine, we have something more for you. You can scroll down to learn more about this topic

For all the practice Videos and Explanation on Python please click over here. Python Practice Series.

Video Explanation to LCM of two numbers

Code: LCM of two numbers

There are various methods to get the LCM of two numbers in python. You can find source code and explanations of different methods over here.

But before that let us understand basic logic obtaining an LCM of two numbers.

  • Initially, Get 2 Integer 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, Use the If condition to check whether the remainder of (max% a) and (max% b) is 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.

Let’s start with our program here.

Method 1: LCM of two numbers using While loop

Source Code and Output

da=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
maximum = max(a, b)
while(1):
    if(maximum %a==0 and maximum %b==0):
        print("LCM is:",maximum)
        break
    maximum=maximum+1

Output:

Enter the first number:5
Enter the second number:10
LCM is:10

Code Explanation Method 1: LCM of two numbers using While loop

Let’s understand how this program works.

First, we are taking two numbers as input from the user and converting them into an integer and we are storing them in variables a and b.

Then we have used max function to get the max number from two numbers.

Then a while loop is used whose condition is always true (or 1) unless break is used.

Then inside the while loop, we have an if statement to check the value in the maximum variable is divisible by both numbers. If it is divisible then we have used the break statement to break the while loop.

In case if it is not divisible by maximum number then the value in maximum variable will be incremented.

This process will run till the maximum number is divisible by both the number and then the print statement will execute and print the LCM of two numbers.

Let’s run this program and pass input as 5 and 10 for the first and second number.

So the resultant output will be 10.

Method 2: LCM of Two Numbers using Recursion

Source Code and Output

def findgcd(a, b):   # Function definition
    if(b == 0):
        return a;
    else:
        return findgcd(b, a % b)   # Recursion takes place here
num1 = int(input("Enter the first number:")) 
num2 = int(input("Enter the second number:"))
gcd = findgcd(num1, num2)   #function call
lcm = (num1 * num2) // gcd
print('LCM is:',lcm)

Output

Enter the first number:5
Enter the second number:10
LCM is: 10

Code Explanation Method 2:  LCM of Two Numbers using Recursion

In this program we have make use of recursion.

We will start from user input, here we have accepted two numbers from the user which are num1 and num2 and converted them into an integer

Then we have call the findgcd function, which will accept two parameters and return GCD of two numbers.

Here we are calling find gcd function recursively. This function will calculate and using swapping numbers and returns GCD.

You can find a detailed explanation of The GCD of two numbers using Recursion.

In the next line we have calculated LCM of two numbers and stored the result into lcm variable.

If we know the greatest common divisor (GCD) of integers num1 and num2, we can calculate the LCM using the following formula.

LCM(a,b)= a×b / GCD(a,b)

So we have multiplied the input variable and then using the floor division operator we have divided it by the GCD of two numbers and stored the result inside lcm.

Let’s run this program and pass input as 5 and 10 for the first and second number.

So the resultant output will be 10.

I am very much sure, now your concept is clear for LCM of two numbers.

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

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