find LCM
Sun Nov 08 2020 23:35:48 GMT+0000 (UTC)
Posted by
@mahmoud hussein
#c++
#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>
using namespace std;
int LCM(int n1, int n2);
int main()
{
cout << "Enter first integer: ";
int n1;
cin >> n1;
cout << "Enter second integer: ";
int n2;
cin >> n2;
cout << "Least Common Multiple for " << n1 <<
" and " << n2 << " is " << LCM(n1,n2)<< endl;
}
int LCM(int n1, int n2)
{
int lcm = 1;
int max = (n1 > n2) ? n1 : n2;
while (true)
{
if (max % n1 ==0 && max % n2 == 0)
{
lcm = max;
break;
}
else
max++;
}
return lcm; // Return gcd
}
content_copy Copy
http://cpp.sh/
Comments