C Program to find LCM of Two Numbers

How to write a C Program to find LCM of Two Numbers with an example?. According to Mathematics, the Least Common Multiple (LCM) of two or more integers is the smallest positive integer that is perfectly divisible by the given integer values without the remainder.

For example, the C LCM value of integer 2 and 3 is 12 because 12 is the smallest positive integer that is divisible by both 2 and 3 (the remainder is 0).

C Program to find LCM of Two Numbers using While Loop

This lcm of two numbers in c program allows the user to enter two positive integer values and we are going to calculate the LCM of those two values using the While Loop.

TIP: Some people will call the Lowest Common Multiple as LCM in C

//C program to find LCM of two numbers
#include <stdio.h>

int main()
{
    int Num1, Num2, max_Value;

    printf("Please Enter two integer Values \n");
    scanf("%d %d", &Num1, &Num2);
    
    max_Value = (Num1 > Num2)? Num1 : Num2;
    
    while(1) //Alway True
    {
    	if(max_Value % Num1 == 0 && max_Value % Num2 == 0) 
    	{
    		printf("LCM of %d and %d = %d", Num1, Num2, max_Value);
    		break;
		}
		++max_Value;
	}
    return 0;
}
Please Enter two integer Values 
15
20
LCM of 15 and 20 = 60

C Program to find LCM of Two Numbers using GCD

This LCM program in C, we are going to calculate the Least Common Multiple of those two values using the While Loop.

TIP: The basic formula behind the Least Common Multiple or LCM in C is: LCM(a, b) = (a * b) / GCD. We already explained the GCD in our previous article. Please refer C Program to find GCD of Two Numbers article in C Programming for more information.

//C program to find LCM of two numbers
#include <stdio.h>

int main()
{
    int Num1, Num2, LCM, Temp, GCD;

    printf("Please Enter two integer Values \n");
    scanf("%d %d", &Num1, &Num2);

    int a = Num1;
    int b = Num2;
    
    while (Num2 != 0) {
 	Temp = Num2;
 	Num2 = Num1 % Num2;
 	Num1 = Temp;
    }
    GCD = Num1;
    printf("GCD of %d and %d = %d \n", a, b, GCD);
    
    LCM = (a * b) / GCD;
    printf("LCM of %d and %d = %d", a, b, LCM);
    return 0;
}
C Program to find LCM of Two Numbers 1

C Program to find LCM of Two Numbers using Recursion

This program for lcm of two numbers in c allows you to enter two positive integer values. Next, we are going to calculate the Lowest Common Multiple of those two values by calling the function recursively. Please refer Recursion in C for further reference

//C program to find LCM of two numbers
#include <stdio.h>
long gcd(long x, long y);
int main()
{
    int Num1, Num2, GCD, LCM;

    printf("Please Enter two integer Values \n");
    scanf("%d %d", &Num1, &Num2);

    GCD = gcd(Num1, Num2);
    LCM = (Num1 * Num2) / GCD;
    printf("LCM of %d and %d is = %d", Num1, Num2, LCM);
    return 0;
}
long gcd(long x, long y) 
{
  if (y == 0) {
  	return x;
  }
  else {
    return gcd(y, x % y);
  }
}

C LCM output

Please Enter two integer Values 
10
25
LCM of 10 and 25 is = 50

The following statement in this program to find lcm of two numbers in c will find the Least Common Multiple using the traditional formula.

LCM = (Num1 * Num2) / GCD;

When the compiler reaches to long gcd(long x, long y) line in main() program lcm example, the compiler will immediately jump to below function:

long gcd(long x, long y)

In the above-specified function, return gcd(y, x % y) statement will help the program to call the function Recursively with the updated value. If you miss this statement in C Program to find LCM of Two Numbers example, then after completing the first line, it will terminate.

Let’s see the If condition if (y == 0) will check whether the number is equal to 0 or not. For Recursive functions, it is very important to place a condition before using the function recursively. Otherwise, we will end up in infinite execution (Same like Infinite Loop). Please be careful :)