Skip to content

Commit f1d71f8

Browse files
committed
GCD and LCM
1 parent 4ec94b3 commit f1d71f8

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

GCDiterative.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@
22
using namespace std;
33

44
/* LCM * HCf = a * b */
5-
5+
/* function to find LCM */
66
int LCM(int a, int b, int gcd) {
7+
/* relation between lcm and hcf/gcd */
78
int lcm = (a * b) / gcd;
89
return lcm;
910
}
1011

12+
/* function to find gcd */
1113
int GCD(int a, int b) {
14+
/* if a = 0 then b is gcd */
1215
if (a == 0) {
1316
return b;
1417
}
18+
19+
/* if b = 0 then a is gcd */
1520
if (b == 0) {
1621
return a;
1722
}
1823

24+
/* subtracting greater number from lower
25+
unless they both are equal
26+
once they are equal return any number a or b*/
1927
while (a != b) {
28+
/* if a is greater subrating b from it */
2029
if (a > b) {
2130
a = a - b;
2231
} else {
32+
/* if b is greater subrating a from it */
2333
b = b - a;
2434
}
2535
}
36+
/* returning gcd */
2637
return a;
2738
}
2839

@@ -33,10 +44,13 @@ int main() {
3344
cout << "Enter B: ";
3445
cin >> b;
3546

47+
/* calling gcd funtion */
3648
int gcd = GCD(a, b);
3749
cout << gcd << endl;
3850

51+
/* calling lcm function */
3952
int lcm = LCM(a, b, gcd);
4053
cout << lcm << endl;
54+
4155
return 0;
4256
}

0 commit comments

Comments
 (0)