# LCM of Three Numbers

## How to Find LCM of three numbers

Please write your version or code in other Languages below in comments.

You will find code in these languages:-

• C
• C++
• Java

If you can write a program to find LCM of two numbers. Then you can very easily write a program to find LCM of three numbers also , cause  lcm(a,b,c)=lcm(a,lcm(b,c))

“>lcm(a,b,c)=lcm(a,lcm(b,c))lcm(a,b,c)=lcm(a,lcm(b,c))

.

So here’s your c program to find LCM of three numbers.

### C Program to find lcm of 3 numbers

[code language=”cpp”]
#include

int lcm(int,int);
int main(){
int a,b,c,l,k;
printf(“Enter any three positive integers “);
scanf(“%d%d%d”,&a,&b,&c);
if(a<b) l = lcm(a,b); else l = lcm(b,a); if(l>c)

k= lcm(l,c);

else

k= lcm(c,l);

printf(“LCM of two integers is %d”,k);

return 0;

}

int lcm(int a,int b){

int temp = a;

while(1){

if(temp % b == 0 && temp % a == 0)

break;

temp++;

}

return temp;
}

[/code]

### Code in C++

[code language=”cpp”]

#include
using namespace std;

int lcm(int, int, int);
int hcf(int, int, int);
int main()
{
int a,b,c;
int LCM, HCF;
cout<<“Enter 1st number: “; cin>>a;
cout<<“Enter 2nd number: “; cin>>b;
cout<<“Enter 3rd number: “; cin>>c;

LCM = lcm(a,b,c);
HCF = hcf(a,b,c);
cout<<“LCM of “<<a<<“,”<<b<<“,”<<c<<” is = “<<LCM<<endl;
cout<<“HCF of “<<a<<“,”<<b<<“,”<<c<<” is = “<<HCF<<endl; return 0; } int lcm(int x,int y, int z) { long max,lcom, count, flag=0; if(x>=y&&x>=z)
max=x;
else if(y>=x&&y>=z)
max=y;
else if(z>=x&&z>=y)
max=z;
for(count=1;flag==0;count++)
{
lcom=max*count;
if(lcom%x==0 && lcom%y==0 && lcom%z==0)
{
flag=1;
}
}
return lcom;
}

int hcf(int p, int q, int r)
{
int gcf=1,flag=0, count;
for(count=1; flag==0;count++)
{
if(p%count==0&&q%count==0&&r%count==0)
gcf=count;
if(count>p&&count>q&&count>r)
{
flag=1;
}
}
return gcf;
}
[/code]

### Java Program to Find LCM of N Numbers

LCM of three Numbers in Java

[code language=”java”]
public class LCM {

public static long lcm(int[] numbers) {
long lcm = 1;
int divisor = 2;
while (true) {
int cnt = 0;
boolean divisible = false;
for (int i = 0; i < numbers.length; i++) {
/**
* lcm (n1,n2,… 0)=0.For negative number we convert into
* positive and calculate lcm.
*/
if (numbers[i] == 0) {
return 0;
} else if (numbers[i] < 0) {
numbers[i] = numbers[i] * (-1);
}
if (numbers[i] == 1) {
cnt++;
}
/**
* divide numbers by devisor if complete division i.e. without
* remainder then replace number with quotient; used for find
* next factor
*/
if (numbers[i] % divisor == 0) {
divisible = true;
numbers[i] = numbers[i] / divisor;
}
}
/**
* If divisor able to completely divide any number from array
* multiply with lcm and store into lcm and continue to same divisor
* for next factor finding. else increment divisor
*/
if (divisible) {
lcm = lcm * divisor;
} else {
divisor++;
}
/**
* Check if all numbers is 1 indicate we found all factors and
* terminate while loop.
*/
if (cnt == numbers.length) {
return lcm;
}
}
}

public static int lcm2(int num1, int num2) {
if(num1==0 || num2==0){
return 0;
}else if(num1<0){
num1=num1*(-1);
}else if(num2<0){
num2=num2*(-1);
}
int m = num1;
int n = num2;
while (num1 != num2) {
if (num1 < num2) {
num1 = num1 + m;
} else {
num2 = num2 + n;
}
}
return num1;
}

public static void main(String[] args) {
int[] numbers = {140, 72, 130};
System.out.println(“*** Least Common Multiple ***”);
System.out.println(“LCM(Least Common Multiple) of N numbers using Table method “);
System.out.println(lcm(numbers));
System.out.println(“LCM of two numbers using repetative addition”);
System.out.println(lcm2(1, 72));

}
}
[/code]

#### Output:

*** Least Common Multiple ***
LCM(Least Common Multiple) of N numbers using Table method
32760
LCM of two numbers using repetative addition
72

### 6 comments on “LCM of Three Numbers”

• swati

LCM of three numbers using function:

#include

int LCM_of_three_num(int n1, int n2, int n3)
{
int LCM, K;
if(n1 > n2 && n1 > n3)
{
LCM = n1;
}
else if(n2 > n1 && n2 > n3)
{
LCM = n2;
}
else if(n3 > n1 && n3 > n2)
{
LCM = n3;
}

K = LCM;

while(1)
{
if(LCM%n1==0 && LCM%n2==0 && LCM%n3==0)
{
break;
}
else
{
LCM = LCM + K;
}
}

return LCM;
}

int main()
{
int n1, n2, n3, Result;
scanf(“%d %d %d”, &n1, &n2, &n3);

Result = LCM_of_three_num(n1, n2, n3);

printf(“LCM(%d, %d, %d) = %d”, n1, n2, n3, Result);
}

OUTPUT:
12 15 10
LCM(12, 15, 10) = 60

• swati

#include

int main()
{
int n1, n2, n3, LCM, K;
scanf(“%d %d %d”, &n1, &n2, &n3);

if(n1 > n2 && n1 > n3)
{
LCM = n1;
}
else if(n2 > n1 && n2 > n3)
{
LCM = n2;
}
else if(n3 > n1 && n3 > n2)
{
LCM = n3;
}

K = LCM;

while(1)
{
if(LCM%n1==0 && LCM%n2==0 && LCM%n3==0)
{
break;
}
else
{
LCM = LCM + K;
}
}

printf(“LCM(%d, %d, %d) = %d”, n1, n2, n3, LCM);
}

OUTPUT:
12 15 10
LCM(12, 15, 10) = 60

• Trishna

#include
using namespace std;
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}

int main() {
// your code goes here
int a,b,c,d,e;
cin>>a;
cin>>b;
cin>>c;
d=(a*b)/gcd(a,b);
e=(d*c)/gcd(d,c);
cout<<e;
return 0;
}

• Alok

LCM of 3 numbers

#include
using namespace std;

int main() {
int n, m, l;
cin>>n>>m>>l;

if(m>n)
{
m=m+n;
n=m-n;
m=m-n;
}
if(l>n)
{
l=l+n;
n=l-n;
l=l-n;
}

int add=n;

while(n%m!=0 || n%l!=0)
{
n+=add;
}

cout<<n;
return 0;
}

• Shubham

def GCD(a, b):

if b == 0:
return a

else:
return GCD(b, a % b)

def LCM(n1, n2):

lcm = (n1*n2)//GCD(n1, n2)

return lcm

n1, n2, n3 = [int(input()) for x in range(3)]

lcm_3 = LCM(n1, LCM(n2, n3)) // GCD(n1, GCD(n2, n3))

print(“LCM OF 3 NOS IS = “, lcm_3)

# DO try this ONE

• Shubham

def GCD(a, b):

if b == 0:
return a

else:
return GCD(b, a % b)

def LCM(n1, n2):

lcm = (n1*n2)//GCD(n1, n2)

return lcm

# print(GCD(60, 48))

n1, n2, n3 = [int(input()) for x in range(3)]

lcm_3 = LCM(n1, LCM(n2, n3)) // GCD(n1, GCD(n2, n3))

print(“LCM OF 3 NOS IS = “, lcm_3)