After finding first common divisor print it
Least common multiple
hi @kumarakash121005
To find the LCM of two numbers, it is obvious that the LCM will be greater than or equal to the samller number in the two given numbers.
Algorithm
- Take input of both the numbers say, N1 and N2.
- Declare a variable with value with the smallest of the two numbers.
- put a Infinite loop
- Inside loop check if the variable can divide both the numbers,
- If yes, break.
- Otherwise increment the variable and continue.
- Print the LCM obtained.
Code
#include<iostream>
int main() {
int num1,num2;
cin >> num1>>num2;
if(num1>num2){
int temp = num2;
num2=num1;
num1=temp;
}
int lcm=num2;
while(lcm>0){
if(lcm%num2==0 && lcm%num1==0){
break;
}
lcm++;
}
cout << lcm << endl;
return 0;
}
We initialise the LCM variable with greatest among the two
No… the smaller of two
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.