cpp program to find LCM
In this program, find the LCM(lowest common multiple) of the two numbers in the cpp programming language.
For understanding this program you should have the knowledge about
code:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, maximum;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
if(num1>num2)
{
maximum=num1;
}
else
{
maximum=num2;
}
while (true)
{
if (maximum % num1 == 0 && maximum % num2 == 0)
{
cout << "LCM = " << maximum;
break;
}
else
maximum++;
}
return 0;
}
Output:
Enter two numbers: 4
3
LCM = 12
Here, take two numbers from the user, find the max number by using if-else statement, In the last apply while loop. In the loop, find the number which is divisible by both numbers, if not then increase the value of maximum until the number is found and the loop is terminated.
If you face any problem in the above code then write problem in the below comment section.
Comments
Post a Comment