The LCM stands for Least Common Multiple , which is used to get the smallest common multiple of two numbers (n1 and n2), and the common multiple should be divisible by the given numbers. A common multiple is a number that is common in both numbers. The representation of the LCM of two numbers, as LCM (a, b) or lcm (a, b).
For example, LCM of two positive numbers, as LCM (12, 24) is 24. Because both numbers 12 and 24 divides 24 without leaving any remainder. Similarly, the LCM of 3 and 4 is 12 because the smallest common multiple of both numbers is 12.
Algorithm of the LCM of two numbers
Step 1: Take two inputs from the user n1 and n2
Step 2: Store the smallest common multiple of n1 and n2 into the max variable.
Step 3: Validate whether the max variable is divisible by n1 and n2, print the max as the LCM of two numbers.
Step 4: Otherwise, the max value is updated by 1 on every iteration, and jump to step 3 to check the divisibility of the max variable.
Step 5: Terminate the program.
Program to get LCM of two numbers using if statement and while loop
Program1.cpp
#include <iostream>
using namespace std;
int main()
{
int n1, n2, max_num, flag = 1;
cout << " Enter two numbers: \n";
cin >> n1 >> n2;
// use ternary operator to get the large number
max_num = (n1 > n2) ? n1 : n2;
while (flag)
{
// if statement checks max_num is completely divisible by n1 and n2.
if(max_num % n1 == 0 && max_num % n2 == 0)
{
cout << " The LCM of " <<n1 << " and " << n2 << " is " << max_num;
break;
}
++max_num; // update by 1 on each iteration
}
return 0;
}
Output
Enter two numbers:
30
50
The LCM of 30 and 50 is 150
Program to get the LCM of two numbers using while loop
Program2.cpp
#include <iostream>
using namespace std;
int main()
{
// declare variables
int num1, num2, lcm, gcd, temp;
cout <<" Enter the first number: ";
cin >> num1;
cout <<" Enter the second number: ";
cin >> num2;
// assign num1 and num2 values to int a and b
int a = num1;
int b = num2;
// use while loop to define the condition
while (num2 != 0)
{
temp = num2;
num2 = num1 % num2;
num1 = temp;
}
// assign num1 to gcd variable
gcd = num1;
lcm = (a * b) / gcd;
cout << "\n LCM of " << a << " and " << b << " = " << lcm;
return 0;
}
Output
Enter the first number: 15
Enter the second number: 10
LCM of 15 and 10 = 30
Program to get the LCM of two numbers using GCD
Program3.cpp
#include <iostream>
using namespace std;
// definition of getGCD() function
int getGCD( int n1, int n2)
{
// here if statement checks the given number is not equal to 0.
if ( n1 == 0 || n2 == 0)
return 0;
// if n1 equal to n2, return n1
if (n1 == n2)
return n1;
// if n1 is greater than n2, execute the followed statement
if ( n1 > n2)
return getGCD (n1 - n2, n2);
return getGCD (n1, n2 - n1);
}
// definition of getLCM() function
int getLCM (int n1, int n2)
{
/* divide the multiplication of n1 and n2 by getGCD() function to return LCM. */
return (n1 * n2) / getGCD (n1,n2);
}
int main()
{
// declare local variables
int n1, n2;
cout << " Enter two positive numbers: ";
cin >> n1 >> n2; // get numbers from user
// print the LCM(n1, n2)
cout << " \n LCM of " <<n1 << " and " << n2 << " is " << getLCM(n1, n2);
return 0;
}
Output
Enter two positive numbers: 50
60
LCM of 50 and 60 is 300
Program to get the LCM of two numbers using recursive function
Program 4.cpp
# include <iostream>
using namespace std;
// define the getGCD() function and pass n1 and n2 parameter
int getGCD( int n1, int n2)
{
if (n1 == 0)
{
return n2;
}
return getGCD(n2 % n1, n1);
}
// define the getLCM function to return the LCM
int getLCM( int n1, int n2)
{
return (n1 / getGCD(n1, n2)) * n2;
}
int main()
{
// declare local variable
int num1, num2;
cout << " Enter two numbers: " <<endl;
cin >> num1 >> num2;
cout << " LCM of two numbers " <<num1 <<" and " << num2 << " is " << getLCM(num1, num2);
}
Output
Enter two numbers:
12
36
LCM of two numbers 12 and 36 is 36
Program to get the LCM of multiple array elements using function and while loop
Program5.cpp
#include <iostream>
using namespace std;
int lcm_num (int n1, int n2)
{
int max;
max = (n1 > n2) ? n1 : n2;
while (true)
{
if (max % n1 == 0 && max % n2 ==0)
return max;
max++;
}
}
// definition of lcm_array() function
int lcm_array (int arr[], int num)
{
int i;
int lcm = lcm_num( arr[0], arr[1]);
// use for loop to get the lcm
for (i = 2; i < num; i++)
{
lcm = lcm_num(lcm , arr[i]);
}
return lcm;
}
int main()
{
// declare integer array
int arr[] = {10, 5, 15, 30};
int num = sizeof(arr) / sizeof(arr[0]); // get the number of elements
cout << " LCM of multiple array elements is: " << lcm_array(arr, num);
}
Output
LCM of multiple array elements is: 30