Skip to content

Commit e2a5364

Browse files
GCD of a number
1 parent 6b7eef8 commit e2a5364

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

gcd.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
int n1, n2, hcf;
6+
cout << "Enter two numbers: ";
7+
cin >> n1 >> n2;
8+
9+
// swapping variables n1 and n2 if n2 is greater than n1.
10+
if ( n2 > n1) {
11+
int temp = n2;
12+
n2 = n1;
13+
n1 = temp;
14+
}
15+
16+
for (int i = 1; i <= n2; ++i) {
17+
if (n1 % i == 0 && n2 % i ==0) {
18+
hcf = i;
19+
}
20+
}
21+
22+
cout << "HCF = " << hcf;
23+
24+
return 0;
25+
}

0 commit comments

Comments
 (0)