Last Updated on May 9, 2023 by Prepbytes

For beginners, LCM of two numbers in C is a very simple program. So, in this article, we will define an LCM and show how to find the LCM of two numbers in C.

## What is LCM?

LCM is an abbreviation for Least Common Multiples. It is the smallest positive number with no remainder that is totally divisible by both numbers n1 and n2. LCM (a, b) or lcm (a, b). The LCM of two positive numbers is 360, for example, 72 and 120.

We can compute the LCM of two numbers, n1, and n2, using one of two methods:

- Making use of a While Loop
- Making use of the gcd Function

Let us now look at how to use these methods to find the LCM of two numbers, n1, and n2.

### Method 1 to Find LCM of Two Numbers in C using While Loop

We can use the while loop along with the if-else statement to find the LCM of two numbers in C by following the steps outlined below.

- Set the positive integer variables A and B to zero.
- Put the common multiple of A and B into the max variable.
- Check to see if the maximum is divisible by both variables A and B.
- Show max as the LCM of two numbers if max is divisible.
- Otherwise, the value of max is increased, and the process returns to step 3.
- Terminate the program.

**Code implementation of finding the LCM of two numbers in C using a while loop**

#include <stdio.h> void main() { int num1, num2, max_div, flag = 1; printf( " Enter any two positive numbers \n "); scanf(" %d %d", &num1, &num2); max_div = (num1 > num2) ? num1 : num2; while (flag) // (flag = 1) { if (max_div % num1 == 0 && max_div % num2 == 0) { printf( " %d ",max_div); break; } ++max_div; // pre-increment max_div } }

**Output Terminal :**

`Enter any two positive numbers. `

**User Input**

`2 3`

**Output**

`6`

### Method 2 to Find LCM of Two Numbers in C using GCD Function

Using the following formula, we can calculate the LCM of two numbers: **LCM = (n1 * n2) / gcd(n1, n2),** where n1 and n2 are the two numbers whose LCM must be found, and gcd is the function used to find the greatest common divisor of two numbers.

Let’s look at how to use gcd in C to find the LCM of two numbers.

- Start
- Define the variables n1, n2, gcd_value, and lcm_value.
- Get the user’s input values for n1 and n2.
- Using the gcd function, compute the gcd_value.
- Determine the lcm_value as (n1 * n2) / gcd_value.
- Print the result as "LCM of n1 and n2 equals lcm_value."
- End

**Code implementation of finding the LCM of two numbers in C using the gcd function**

#include <stdio.h> int gcd(int n1, int n2) { if (n2 != 0) { return gcd(n2, n1 % n2); } else { return n1; } } int lcm(int n1, int n2) { int gcd_value = gcd(n1, n2); return (n1 * n2) / gcd_value; } int main() { int n1, n2; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); printf("%d\n", lcm(n1, n2)); return 0; }

**Output Terminal :**

`Enter two positive integers: `

**User Input**

`2 3`

**Output**

`6`

**Conclusion**

In summary, the LCM (Least Common Multiple) is the lowest positive number that is divisible by two given integers. In C, there are two ways to calculate the LCM: a while loop and the gcd (Greatest Common Divisor) function. Both ways have been illustrated using example code. The while loop checks for common multiples, whereas the gcd function finds the gcd first and then calculates the LCM.

## Frequently Asked Questions (FAQs)

**Q1. What is the full form of LCM in C programming?**

**Ans.** L.C.M. or Least Common Multiple of two values, is the smallest positive value which is the multiple of both values.

**Q2. Can we find LCM by using any method?**

**Ans.** The Prime Factorization Method and the Division Method are the two main ways for determining the LCM (Least Common Multiple) and the HCF (Highest Common Factor) of numbers.

**Q3. What is the LCM of 24 or 32?**

**Ans.** 96