Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions CPP/Basics/LCM_finder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//C++ program to find the LCM using the HCF of the numbers
//This algorithm finds LCM of two numbers in logarithmic time complexity
#include <bits/stdc++.h>
using namespace std;
// Function to compute the HCF, to further use in finding the LCM

int Find_HCF(int a, int b){
while(a>0 && b>0){
// If a > b then a%b
if(a>b){
a = a%b;
}
// Else b%a
else{
b = b%a;
}
}
// When the loop exhausts, the non zero number will be the HCF
if(a==0){
return b;
}
else{
return a;
}
}
int Find_LCM(int a, int b){
//Using the formula lcm=a*b/hcf(a,b),
return a*b/Find_HCF(a,b);
}

int main(){
// Input two numbers
int a,b;
cin >> a >> b;
// Calling Find_LCM function to find the LCM,
cout << Find_LCM(a,b) ;
}


//Time Complexity = O(log(min(a,b)))
//Space Complexity = O(1)
Binary file added a.exe
Binary file not shown.