// C++ program to find LCM of two numbers #include <iostream> using namespace std; // Recursive function to return gcd of a and b long long gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } // Function to return LCM of two numbers // Time Complexity: O(log(min(a,b) // Auxiliary Space: O(log(min(a,b)) long long lcm(int a, int b) { return (a / gcd(a, b)) * b; } // Function to return LCM of two numbers Time Complexity: O(min(a,b)) int LCM(int a, int b) { int greater = max(a, b); int smallest = min(a, b); for (int i = greater; ; i += greater) { if (i % smallest == 0) return i; } } // Driver program to test above function int main() { int a = 15, b = 20; cout <<"LCM of " << a << " and " << b << " is " << lcm(a, b); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter