i am trying to find the LCM of a number using the following formula. Lcm = Gcd/(a*b). This is working fine for small number, however for larger numbers it overflows, like the one shown in the code. I tried using long long as the variable type but still no effect. How do i fix the overflow issue?
#include <iostream>
#include <vector>
using namespace std;
long long int LCM(int n1, int n2){
const int size = 2;
long long int sum;
long long int gcd;
long long int lcm = 0;
vector<int> number(2);
number[0] = n1;
number[1] = n2;
while (true)
{
sum = number[0] % number[1];
gcd = number[1];
if (sum == 0)
break;
number[0] = number[1];
number[1] = sum;
}
lcm = ((n1*n2)/gcd);
return lcm;
}
int main()
{
cout << LCM(28851538, 1183019) << endl;
system("pause");
}