C Program To Find LCM Of Two Numbers

Last updated on Sep 26,2020 3.2K Views

C Program To Find LCM Of Two Numbers

edureka.co

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,

  1. Understand the concept of LCM
  2. Understanding the Algorithm
  3. Let’s code

 So let us get started then,

C Program To Find LCM Of Two Numbers

Understand the concept of LCM

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.

Understanding the Algorithm

While understanding the algorithm we will look at different approaches to reduce the complexity and increase the efficiency of our program. Let’s start.

We have made it to the final bit of this ‘C Program To Find LCM Of Two Numbers’ article

Let’s Code: C Program To Find LCM Of Two Numbers

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);&nbsp; /*Asking input from the user*/
if(a>b)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*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.

BROWSE COURSES