Introduction
In this post, I will teach you to write a program to find LCM of two numbers in c programming. The LCM stands for Least Common Multiple.
Also Read: GCD of Two Numbers in C Programming
Before writing a program in c to find LCM of two numbers, let’s know, what is exactly LCM?
LCM of Two Numbers
As I said, LCM is nothing but Least Common Multiple. Suppose, we have two numbers, say a and b, then we need to find the common multiples of a and b. Suppose, c, d, e and h are multiples of a and d, f, g and h are multiples of b. So, which are the common multiples of a and b? Here, you can see, d and h are common multiples of a and b. But LCM can be only one but we have two common multiples here. So, what to do now? That is why the word “least” come here. We just need to find the smallest number between d and h. If d is the smallest between d and h, then d is the LCM of a and b, otherwise, h is the LCM. So simple, isn’t it?
Let’s understand with some examples.
LCM of 6 and 8
Here, we have two numbers i.e. 6 and 8. So, we need to find the least common multiple of both the numbers.
Also Read: C Program to Find the Sum of Cubes of Elements in an Array
Multiples of 6 are 6, 12, 18, 24, 30, 36 and so on whereas multiples of 8 are 8, 16, 24, 32, 40, 48 and so on. So, here you can find 24 is the only common multiple of 6 and 8. That means both 6 and 8 can divide both 24. There are many common multiples of both 6 and 8 but 24 is the least common multiple or we can say, in the above list of multiples of 6 and 8, 24 comes first.
Look at another examples.
LCM of 6 and 9
Multiples of 6 are 6, 12, 18, 24, 30, 36 and so on whereas multiples of 9 are 9, 18, 27, 36, 45 and so on. This list can be so long, but I am mentioning here only a few multiples. So, here you can find 18 and 36 are the common numbers between two the list of multiples of 6 and 9. But, we need the smallest number and here, that number is 18. So, therefore, 18 is the LCM of 6 and 9.
LCM of 9 and 12
Multiples of 9 are 9, 18, 27, 36, 45, 54, 63, 72 and so on whereas multiples of 12 are 12, 24, 36, 48, 60, 72, 84 and so on. So, here you can find 36 and 72 are the common numbers between two the list of multiples of 9 and 12. But, we need the smallest number and here, that number is 36. So, therefore, 36 is the LCM of 9 and 12.
Also Read: C Program to Print Numbers Except Multiples of n
I think, these are enough examples to understand how to find LCM of two numbers and we will use this to write a program in c.
Logic to find LCM of Two Numbers in C Programming
In the above examples, I have explain a very simple method to find LCM of two numbers. But, we have to write a c program to find the LCM of two numbers and this simple logic won’t help. So, I have a very good logic to solve this problem.
Suppose, we need to find LCM of 6 and 8. Just follow the following steps:
- Find the greater and smaller number between 6 and 8. We know 6 is smaller and 8 is greater. But, I am writing this step according to our c program because we don’t know what value will user enter.
- Now, divide greater number by smaller number and find the remainder. In this case, divide 8 by 6 and the remainder will be 2.
- If the remainder is 0, then we can say 8 is the LCM of 6 and 8. But remainder is 2, not 0.
- If the remainder is not 0, then take next multiple from the greater number and again divide that multiple by the smaller number. Here, divide 16 by 6. Where this 16 come from? As I have already said, take next multiple from greater number. So, 16 is the next multiple of 8.
- Now, repeat the above steps until we get remainder 0.
- When you divide 16 by 6, you will get remainder 4 but not 0. So, again take next multiple from the table of 8 and that number is 24.
- Now, divide 24 by 6 and you will get remainder 0.
- You can directly say that, 24 is the LCM of 6 and 8.
That means both 6 and 8 can divide 24.
Also Read: Arrays in C for Complete Beginners
I hope, you have understood this logic. Now, we will use this logic to write a program in c to find LCM of two numbers.
C Program to find LCM of two Numbers.
#include<stdio.h>
int main()
{
int n1,n2,greater,smaller,i=1,lcm;
printf("Enter two numbers\n");
scanf("%d%d",&n1,&n2);
if(n1>n2)
{
greater=n1;
smaller=n2;
}
else
{
greater=n2;
smaller=n1;
}
while(1)
{
lcm = greater * i;
if((lcm%smaller)==0)
{
printf("LCM of %d and %d is %d", n1,n2,lcm);
break;
}
i++;
}
return 0;
}
When you compile and run the above c program, you will be able to find LCM of two numbers. Now, see the following output.
Output
Enter two numbers
6
8
LCM of 6 and 8 is 24
I don’t think, I need to explain this program. Because, if you understood the logic, then I can guarantee that you have understood this program.
Also Read: C Program to Remove Zeros from a number
If you know Hindi language, then I have made a video on this topic. So, I am sharing that video here and I hope you will like that video too.
Thanks for reading and watching.