-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCM.cpp
More file actions
39 lines (35 loc) · 794 Bytes
/
LCM.cpp
File metadata and controls
39 lines (35 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Add some code
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--)
{
int a, b, gcd = 0;
long long int lcm = 0;
cin >> a >> b;
int min = (a < b) ? a : b;
// Calculate the Greatest Common Divisor (GCD)
for (; min >= 1; min--)
{
if (a % min == 0 && b % min == 0)
{
gcd = min;
break;
}
}
if (gcd == 0)
{
// Both 'a' and 'b' are zero; LCM is also zero.
cout << "LCM = 0\n";
}
else
{
lcm = static_cast<long long int>(a) * b / gcd; // Ensure proper type casting
cout << "LCM = " << lcm << "\n";
}
}
return 0;
}