Skip to content

Commit 6c5c93f

Browse files
authored
Merge pull request #216 from anujparwal/main
Added LCM finder
2 parents 0712b58 + 347bed1 commit 6c5c93f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

CPP/Basics/LCM_finder.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//C++ program to find the LCM using the HCF of the numbers
2+
//This algorithm finds LCM of two numbers in logarithmic time complexity
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
// Function to compute the HCF, to further use in finding the LCM
6+
7+
int Find_HCF(int a, int b){
8+
while(a>0 && b>0){
9+
// If a > b then a%b
10+
if(a>b){
11+
a = a%b;
12+
}
13+
// Else b%a
14+
else{
15+
b = b%a;
16+
}
17+
}
18+
// When the loop exhausts, the non zero number will be the HCF
19+
if(a==0){
20+
return b;
21+
}
22+
else{
23+
return a;
24+
}
25+
}
26+
int Find_LCM(int a, int b){
27+
//Using the formula lcm=a*b/hcf(a,b),
28+
return a*b/Find_HCF(a,b);
29+
}
30+
31+
int main(){
32+
// Input two numbers
33+
int a,b;
34+
cin >> a >> b;
35+
// Calling Find_LCM function to find the LCM,
36+
cout << Find_LCM(a,b) ;
37+
}
38+
39+
40+
//Time Complexity = O(log(min(a,b)))
41+
//Space Complexity = O(1)

a.exe

45.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)