Skip to content

Commit 1bebd29

Browse files
committed
LCM of 2 Digits
1 parent 276892d commit 1bebd29

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

LcmUsingRecursion.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// Function declaration
5+
int Findlcm(int x, int y);
6+
7+
int main()
8+
{
9+
int num1, num2, LCM;
10+
11+
// Inputting two numbers from user
12+
cout << "Enter any 2 numbers to find LCM: " << endl;
13+
cin >> num1;
14+
cin >> num2;
15+
16+
if (num1 > num2)
17+
LCM = Findlcm(num2, num1);
18+
else
19+
LCM = Findlcm(num1, num2);
20+
21+
cout << "LCM of " << num1 << " and " << num2 << " is: " << LCM;
22+
23+
return 0;
24+
}
25+
26+
int Findlcm(int x, int y)
27+
{
28+
static int multiple = 0;
29+
30+
// Increments multiple by adding max value to it
31+
multiple += y;
32+
33+
if ((multiple % x == 0) && (multiple % y == 0))
34+
{
35+
return multiple;
36+
}
37+
else
38+
{
39+
return Findlcm(x, y);
40+
}
41+
}

0 commit comments

Comments
 (0)