forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcm.cpp
54 lines (42 loc) · 822 Bytes
/
lcm.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Using GCD, find LCM of two number.
#include <iostream>
using namespace std;
// Gcd recursive Function
long long gcd(long long int x, long long int y)
{
if (y == 0)
return x;
//Rcursive function
return gcd(y, x % y);
}
// Lcm Function
long long lcm(int a, int b)
{
//return Lcm of two number
return (a / gcd(a, b)) * b;
}
//main function
int main()
{
//Input 2 numbers for Lcm
int n1, n2;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
//Print output
cout<<"\tOUTPUT\n";
//calling Lcm Funtion
cout << lcm(n1, n2);
return 0;
}
/*Sample Input Output
Sample 1.
Enter two numbers : 761457 614573
OUTPUT
LCM of 761457 and 614573 --> 467970912861
Sample 2.
Enter two numbers : 6 8
OUTPUT
LCM of 6 and 8 --> 24
Time Complexity :O(logn)
Space Complexity:O(1)
*/