Python Certification Training for Data Scienc ...
- 71k Enrolled Learners
- Weekend/Weekday
- Live Class
Finding the LCM of the 2 numbers is something that we learned long back and still use its application in our day to day life. In this post, we will develop a deep understanding of this fundamental concept of LCM and Implement a C program to find LCM of 2 numbers.
Following are pointers covered in this article,
LCM stands for Lowest Common Multiple. It is the lowest possible number which is divisible by both the numbers we are considering. Let’s not stick to the definition, and dug deeper into the concept.
Let’s consider 2 numbers 4 and 15 and see their respective Multiples
4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 | 44 | 48 | 52 | 56 | 60 |
15 | 30 | 45 | 60 | 75 | 90 | 105 | 120 | 135 | 150 | 165 | 180 | 195 | 210 | 235 |
In the table above I’ve mentioned a few multiples of 4 and 15. Now, Let’s find the LCM. To accomplish that we need to find the least possible number which is divisible by both the numbers after looking at the table it doesn’t look like a tough task, and we can easily figure out the answer which in this case is 60. But, making such tables is not always feasible. Hence, creating a program will save us from making these huge tables and ample time.
While understanding the algorithm we will look at different approaches to reduce the complexity and increase the efficiency of our program. Let’s start.
Firstly, we need to consider 2 values for LCM which is positive integers. Let’s consider a and b
Now, Let’s think about what could be the least possible value of LCM for any 2 numbers, for now, we will consider it as 1 and highest possible value of LCM. You’ll agree with me on the previous statement. As the product of 2 number is a multiple of both of them. Hence, it may not be LCM but definitely a common multiple.
Now, we will run a loop for all the values between 1 and a * b, in the loop, we will check if the number is completely divisible by a and b. Once we find the number we will print its value.
If we implement the above 3 steps in our program it will run fine but let’s go one step further and implement what we’ve learned so far, we considered the lowest possible value of LCM as 1 but can the value of LCM be less than a or b? No. If still, you’re thinking how it can’t be less than a and b, refer the table above. So, in our loop, we won’t check for values less than a or b.
For further modifications, we can change the increment value to the multiples of the largest of the 2 numbers, as numbers between them can’t be LCM.
We have made it to the final bit of this ‘C Program To Find LCM Of Two Numbers’ article
So far we have learned all the essential concepts to write a code which can find LCM of 2 numbers. Now, let’s use the above Algorithm and write a program to find LCM of 2 numbers.
#include <stdio.h> #include <conio.h>int main(){ int a; int b; int GreatestNumber; int PossibleLCM;printf("Enter 2 positive integers:"); scanf("%d%d", &a, &b); /*Asking input from the user*/ if(a>b) /*Checking for the greatest number*/ GreatestNumber = a; else GreatestNumber = b; for(PossibleLCM = GreatestNumber; PossibleLCM<=a*b; PossibleLCM = PossibleLCM+GreatestNumber)
Output
When we find the LCM of 4 and 15 using our program we get 60 which is the correct answer, Play around with the code and try to achieve similar results with fewer lines of code. Kudos, this program will be a lifesaver for the next time when you’ll find LCM of 2 numbers.
With this we come to the end of this article on ‘C Program To Find LCM Of Two Numbers’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.
Got a question for us? Mention them in the comments section of this article on and we will get back to you.