Monday, November 29, 2021
HomeTop Coding QuestionWrite a program to lCM of two number.

Write a program to lCM of two number.

Given a two number ,we need to find the lcm of two number .and print their resultant value.

So what is LCM of number?

LCM of two number is smallest number that divides both the number.

Example:

input:15 and 25
ouput:75

Logic:

we just need to find the smallest number that divides both the number.

Code-

#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b); 
	
}
int lcm(int a, int b)
{
    return (a / gcd(a, b)) * b;
}
  
int main()
{
  int a=15;
  int b=25;
    int k;
 k=lcm(a,b);
	cout<<k<<endl;

}

Output:

75

Time complexity:0(N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments