LCM and HCF

PHOTO EMBED

Sat Dec 23 2023 16:08:32 GMT+0000 (Coordinated Universal Time)

Saved by @coderworld

// 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; 
} 
content_copyCOPY