➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
In this post, we will develop a program to find LCM of two numbers in C++ programming language.
Least or lowest common multiple (LCM) of two integers a and b is the smallest positive number that is divisible by both a and b. Example:- LCM of 12 and 15 is 60 because 60 is divisible by both 12 and 15.
// C++ program to find LCM
#include<iostream>
using namespace std;
int main()
{
// declare variables
int num1, num2, max;
// take input
cout << "Enter two Integers: ";
cin >> num1 >> num2;
// maximum value between num1 and num2
max = (num1 > num2) ? num1 : num2;
while(true)
{
if(max % num1 == 0 && max % num2 ==0)
{
cout << "LCM = " << max << endl;
break;
}
else
max++;
}
return 0;
}
Output for the different test-cases:-
Enter two Integers: 12 14
LCM = 84
Enter two Integers: 12 16
LCM = 48
Another program
// C++ program to find LCM
#include<iostream>
using namespace std;
int main()
{
// declare variables
int n1, n2, minMultiple;
// take input
cout << "Enter two Integers: ";
cin >> n1 >> n2;
// find smallest and largest number
int smallest = (n1 < n2) ? n1 : n2;
int largest = (n1 > n2) ? n1 : n2;
// assign smallest number to minMultiple
minMultiple = smallest;
// loop
while(true)
{
if(minMultiple % largest == 0)
{
// display result
cout << "LCM = " << minMultiple <<endl;
break;
}
minMultiple = minMultiple + smallest ;
}
return 0;
}
LCM of Two Numbers in C++ Using HCF
We can also find the LCM of two numbers in C++using HCF or GCD. For this we have to use the following Formula, The product of two numbers a and b is equal to the product of HCF(a,b) and LCM(a,b).
a*b = HCF(a,b) * LCM(a,b)
In the below program to find LCM of two numbers in C++; First, we find the HCF then using the above formula LCM will be calculated. The Formula used for this purpose is,
LCM(a,b) = (a*b) / HCF(a,b)
// C++ program to find LCM using HCF
#include<iostream>
using namespace std;
int main()
{
// declare variable
int num1, num2, hcf, temp, lcm;
// take input
cout << "Enter two Integers: ";
cin >> num1 >> num2;
// assign values
hcf = num1;
temp = num2;
// calculate HCF
while (hcf != temp)
{
( hcf > temp ) ? (hcf -= temp) : (temp -= hcf);
}
// calculate LCM
lcm = (num1 * num2) / hcf;
// display result
cout << "LCM = " << lcm << endl;
return 0;
}
LCM of Two Numbers in C++ Using Recursion
We can also use the recursion technique to find the lcm of two numbers. A technique of defining the method/function that contains a call to itself is called the recursion.
The recursive function/method allows us to divide the complex problem into identical single simple cases that can be handled easily. This is also a well-known computer programming technique: divide and conquer.
// C++ program to find LCM using HCF
#include< iostream>
using namespace std;
// global variable
static int common;
// function declaration
long lcm(int, int);
// main function
int main()
{
// declare variable
int num1, num2;
// take input
cout << "Enter two Integers: ";
cin >> num1 >> num2;
// display result
cout << "LCM = " << lcm(num1, num2) << endl;
return 0;
}
// function to find LCM value
long lcm(int n1, int n2)
{
// increase common
common += n2;
if(common % n1 == 0)
return common; // base case
else
return lcm(n1, n2); //general case
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Also See:-
- Program to Reverse a Number in C++
- Sum of Natural Numbers in C++
- C++ Program for GCD of Two Numbers
- Find Power of a Number in C++
- Fibonacci Series Program in C++
- Check Palindrome Number in C++
- Factorial of a Number Program in C++
- Factorial Using Recursion in C++
- Prime Number Program in C++
- Prime Number b/w 1 to N in C++