LCM is a mathematical concept denoting the Least Common Multiple. This value is the smallest positive integer that can be divided evenly by both integer values n1 and n2, with no remainder. Another term for LCM is the Lowest Common Multiple. It is typically denoted as LCM(a, b) or lcm(a, b). For instance, if we consider two positive integers, 72 and 120, the LCM would be 360.
Algorithm of LCM
Following are the steps to calculate the Least Common Multiple (LCM) of two numbers:
Initialize the integer variables A and B with positive values.
Step 2: Save the shared multiple of A and B in the max variable.
Step 3: Confirm if the maximum value can be evenly divided by both variables A and B.
Step 4: If the maximum number is divisible by the other number, then show the maximum number as the Least Common Multiple (LCM) of the two numbers.
Step 5: Otherwise, increment the value of max, and proceed to step 3.
Step 6: Stop the program.
LCM of two numbers using while loop
Let's explore a demonstration to calculate the Least Common Multiple (LCM) of two numbers in the C programming language by utilizing a while loop.
Lcm.c
#include <stdio.h>
#include <conio.h>
void main()
{
int num1, num2, max_div, flag = 1;
// accept any two positive number from the user
printf( " Enter any two positive numbers to get the LCM \n ");
scanf(" %d %d", &num1, &num2);
// max_div variable holds the max divisible number between num1 and num2.
max_div = (num1 > num2) ? num1 : num2;
while (flag) // (flag = 1)
{
if (max_div % num1 == 0 && max_div % num2 == 0)
{
printf( " The LCM of %d and %d is %d. ", num1, num2, max_div);
break;
}
++max_div; // pre-increment max_div
}
}
Output
Enter any two positive numbers to get the LCM
15
12
The LCM of 15 and 12 is 60.
As demonstrated in the program above, we have provided two integers, 15 and 12, assigned to variables num1 and num2. The maxdiv variable retains the greatest common divisor of num1 and num2. It is important to note that the least common multiple (LCM) of two numbers will always be greater than or equal to the maxdiv.
During every loop iteration, the variable max_div examines if the number is divisible by the variables num1 and num2 within the if statement.
if (max_div % num1 == 0 && max_div % num2 == 0)
{
}
If the condition mentioned above is false, the max_div variable will be incremented by 1, and the loop iteration will persist until the condition specified in the if statement becomes true.
LCM of two numbers using GCD
Let's explore a C program that calculates the Least Common Multiple (LCM) of two numbers by utilizing the Greatest Common Divisor (GCD).
Gcd.c
#include <stdio.h>
#include <conio.h>
int main()
{
// declaration of the local variables
int num1, num2, i, gcd, LCM;
printf (" Enter any two positive numbers: \n ");
scanf (" %d %d", &num1, &num2);
/* use for loop to define the num1 & num2. Where the num1 and num2 should be equal or less than i. */
for ( i = 1; i <= num1 && i <= num2; ++i)
{
// check whether i id divisible by both positive number num1 and num2.
if (num1 % i ==0 && num2 % i == 0)
gcd = i; // assign the divisible number i to gcd
}
LCM = (num1 * num2) / gcd;
printf( " The LCM of two numbers %d and %d is %d. ", num1, num2, LCM );
}
Output
Enter any two positive numbers:
30
20
The LCM of two numbers 30 and 20 is 60.
LCM of two numbers using function
Let's explore a C program that calculates the Least Common Multiple (LCM) of two numbers by utilizing functions.
Max.c
#include <stdio.h>
#include <conio.h>
int get_lcm( int a, int b); // function declaration
int main()
{
int n1, n2, lcm; // declaration of variables
printf (" \n Enter any two positive numbers to get the LCM of: \n ");
scanf ("%d %d", &n1, &n2);
lcm = get_lcm( n1, n2); // function calling
printf ( " \n LCM of %d and %d is %d. ", n1, n2, lcm);
return 0;
}
int get_lcm ( int n1, int n2) // function definition
{
/* static variabe is iniatialized only once for each function call */
static int max = 1;
if ( max % n1 == 0 && max % n2 == 0)
{
return max;
}
else
{
max++;
get_lcm( n1, n2);
return max;
}
}
Output
Enter any two positive numbers to get the LCM of:
30
25
LCM of 30 and 25 is 150.
LCM of two numbers using recursive function
Let's explore a program in C that calculates the Least Common Multiple (LCM) of two numbers by utilizing a recursive function.
Lcm_fun.c
#include <stdio.h>
#include <conio.h>
// use Recursive function to return gcd of two numbers num1 and num2.
int gcd( int num1, int num2)
{
if ( num1 == 0) // if num1 is equal to 0, return num2
{
return num2;
}
return gcd (num2 % num1, num1);
}
// lcm_fun () function returns the LCM of two numbers
int lcm_fun( int num1, int num2)
{ // divide the num1 by gcd() function and then multiply with num2.
return ( num1 / gcd(num1, num2)) * num2;
}
int main()
{ // declaration and initialization of positive numbers
int num1, num2;
printf( "Enter any two positive numbers \n");
scanf(" %d %d", &num1, &num2);
printf ( " LCM of two numbers %d and %d is %d ", num1, num2, lcm_fun( num1, num2));
return 0;
}
Output
Enter any two positive numbers
26
20
LCM of two numbers 26 and 20 is 260