Skip to content

Commit ff2f65a

Browse files
authored
Merge pull request #23 from 22PoojaGaur/smallest-multiple
#11 Smallest multiple solution in c++
2 parents 1e4eda3 + bf493b7 commit ff2f65a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Solutions/Smallest_multiple.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int gcd(int a, int b) {
6+
if(b == 0)
7+
return a;
8+
return gcd(b, a % b);
9+
}
10+
11+
int main() {
12+
int T, N, lcm;
13+
cin >> T;
14+
for(int t=0; t<T; t++) {
15+
cin >> N;
16+
lcm = 1;
17+
// using lcm(a, b) = (a * b)/gcd(a, b)
18+
for (int i = 2; i <= N; i++) {
19+
lcm = ((i * lcm) / gcd(i, lcm));
20+
}
21+
cout << lcm << endl;
22+
}
23+
return 0;
24+
}

0 commit comments

Comments
 (0)