Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

LCM of Two Numbers in C

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.

  1. Set the positive integer variables A and B to zero.
  2. Put the common multiple of A and B into the max variable.
  3. Check to see if the maximum is divisible by both variables A and B.
  4. Show max as the LCM of two numbers if max is divisible.
  5. Otherwise, the value of max is increased, and the process returns to step 3.
  6. 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.

  1. Start
  2. Define the variables n1, n2, gcd_value, and lcm_value.
  3. Get the user’s input values for n1 and n2.
  4. Using the gcd function, compute the gcd_value.
  5. Determine the lcm_value as (n1 * n2) / gcd_value.
  6. Print the result as "LCM of n1 and n2 equals lcm_value."
  7. 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

Leave a Reply

Your email address will not be published. Required fields are marked *