WAP (Python) to find the LCM of two numbers - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Saturday 3 October 2020

WAP (Python) to find the LCM of two numbers


Hey all, in this program, we are going to find the LCM (Least Common Multiple) of two numbers in Python. So, get ready to do this problem in the best way possible!


As always, you could find the final code at the end of this article. But it would be good if you follow along with us the whole journey, as we are going to go through each and every step you need to follow to become the best coder in the world!!!


Also read: Writing some fun programs using 'Random' Module in Python


What is LCM?


LCM stand for Least Common Multiple. In simple terms it is the smallest common multiple of both the numbers. Finding LCM is important when solving questions related to fractions.


For example: To find the LCM of 8 and 12:


First we write the table of 8: 8, 16, 24, 32, 40, 48, 56.......

Then the table of 12: 12, 24, 36, 48, 60, 72, 84.......


It is clearly visible that the least common multiple of both the numbers is 24. Hence, 24 is its LCM!


I hope that now you understood the meaning of LCM. Ready to put this knowledge into code? Let's start!


Overview: 


In this program, we are asked to find the LCM of the two numbers entered by the user. So the first step would be to ask for the input and then we would use simple while loops to find the answer. In the while loop, we would be checking if a particular number is divisible by both numbers, if it does, then it would be our LCM and if it doesn't then the value of that number would be increased by 1 and the loop will run on.


*As I have already given you the basic understanding of the solution, you could try to do it on your own.*


Understanding what we need to do:


Here, I will tell you step-wise what you need to do.


Step 1: Asking the user for the input:


This is the easiest step. We will declare two variable namely 'num1' and 'num2' for storing the two inputs. And don't forget to typecast the input into integer using the 'int' command as we need to do calculations on it.


Here's the code for this step:


Step 2: Finding the LCM of two numbers:


Now, it is a known fact that the LCM of the two numbers can never be less than the bigger number. It is always 'equal to' or 'greater than' the bigger number. Hence we have to make another variable called 'max_num' that stores the value of the bigger number. We used the 'max' function for it.


There is no set value for range of the 'while' loop, hence we kept it 'True' so that it could run till infinity. Now we need to understand that the LCM would be the smallest number that gets divided completely by both the given numbers. 


Hence, we will write an 'if' statement to check if the 'max_num' is divisible by both the numbers. We would do that using the '%' operator. If both the conditions satisfies, then we would break the loop and then print that the current 'max_num' is the LCM. But if it doesn't, we would increment the value of 'max_num' by 1 and continue the loop until the condition is satisfied!


Here's the code for this step:


*Note: Here we have used the 'and' keyword so that the condition would be satisfied if and only if it holds true for both of them.*


So, we have successfully found the best way to find the LCM of two numbers in Python!


Here's the final code:


And here ends our amazing journey, where we found the best way to find the LCM of two numbers in Python. I really hope you liked this article and if you did then don't forget to share it. This can really help someone struggling with this question.


Also read: WAP (Python) to print the nth term of the Fibonacci Series


Also, please consider subscribing to our newsletter as we do these amazing coding sessions regularly!


Again, Thanks for Reading!

No comments:

Post a Comment