# C Program To Find LCM of Two Numbers

In this post, we will learn to calculate the LCM of two numbers using C Programming language.

The LCM (Least Common Multiple) of two numbers a and b is the smallest positive integer that is exactly divisible by both a and b. For example, the lcm of 6 and 26 is 78.

So, without further ado, let’s begin the tutorial.

## C Program to Find LCM of two NumbersUsing Loop

```// C Program to Find LCM of Two Numbers
#include <stdio.h>
int main(){
int a, b, lcm;

printf("Enter two numbers: \n");
scanf("%d %d", &a, &b);

// logic
lcm = (a > b) ? a : b;

while(1){
if (lcm % a == 0 && lcm % b == 0)
break;
lcm++;
}

// Displays Output
printf("The LCM of %d and %d is: %d", a, b, lcm);
return 0;
}
```

Output

``````Enter two numbers:
6 27
The LCM of 6 and 27 is: 54
``````

## How Does This Program Work ?

``````    int a, b, lcm;
``````

In this example, we have declared three int type variables a, b and lcm.

``````    // Asking for Input
printf("Enter two numbers: \n");
scanf("%d %d", &a, &b);
``````

Then , the user is asked to enter two numbers. The value of these two numbers will get stored in a and b variables respectively.

``````    // logic
lcm = (a > b) ? a : b;

while(1){
if (lcm % a == 0 && lcm % b == 0)
break;
lcm++;
}
``````

The maximum of two numbers will be stored in lcm and inside the loop. Then, we will check whether lcm is divisible by both the numbers or not.

If both are divisible by lcm, then Current value of the lcm will be the LCM of two numbers, otherwise we increment the value of lcm and repeat the process until lcm is divisible by both.

``````    // Displays Output
printf("The LCM of %d and %d is: %d", a, b, lcm);
return 0;
``````

Then, we will print the LCM of two numbers using printf() function.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user.

printf() – printf() function is used to display and print the string under the quotation to the screen.

while Loop – In the while loop, the statements inside the body of the while loop keeps executing until it is evaluated to False.

// – Used for Commenting in C.

## Finding LCM Using GCD

```// Finding LCM Using GCD
#include <stdio.h>
int main(){
int a, b, i, gcd, lcm;

printf("Enter two numbers: \n");
scanf("%d %d", &a, &b);

// logic
for (i = 1; i < a && i < b; ++i){
if (a % i == 0 && b % i == 0)
gcd = i;
}
lcm = (a * b) / gcd;

// Displays Output
printf("The LCM of %d and %d is: %d", a, b, lcm);
return 0;
}
```

Output

``````Enter two numbers:
12 16
The LCM of 12 and 16 is: 48
``````

LCM of two numbers can also be found using the formula (a * b) / GCD.

## Conclusion

I hope after reading this post, you understand how to find LCM of two numbers using the C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.