LCM of Three Numbers C Program asked in Wipro NLTH
Ques: How to Find LCM of three numbers.
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.
Please write your version or code in other Languages below in comments.
C
									C++
									JAVA
									Python
							C
					
Run
#include <stdio.h>
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;
}C++
					
Run
#include <iostream>
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;
}JAVA
					
Run
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));
}
Python
					Code is not available yet, comment down the code below the page in the comment section using the same logic or different approach you want to.

 
                             
                        
def LCM_2_num(a, b):
lcm = 0
if a > b:
for j in range(1, 100001):
if (a * j) % b == 0:
lcm = a * j
break
elif a < b:
for j in range(1, 100001):
if (b * j) % a == 0:
lcm = b * j
break
else:
lcm = a
return lcm
def LCM(*num):
lcm = 0
for i in range(0, len(num)):
if i == 0:
lcm = LCM_2_num(num[i], num[i+1])
else:
lcm = LCM_2_num(lcm, num[i])
return lcm
lc = LCM(24, 36, 72)
print(lc)
#Program to print LCM of 3 numbers
#Take 3 numbers as Input
a=int(input())
b=int(input())
c=int(input())
#Function to calculate LCM, function call will return LCM
def LCM(a,b,c):
product=a*b*c
if a>b and a>c:
m=a
elif b>a and b>c:
m=b
else:
m=c
for i in range(m,product+1):
count=0
if i%a==0:
count=count+1
if i%b==0:
count=count+1
if i%c==0:
count=count+1
if count==3:
break
return i
res=LCM(a,b,c)
#Printing result
print(res)
import java.util.Scanner;
public class LCM{
public static void main(String args[]){
Scanner s= new Scanner(System.in);
int a,b,c,g,p,i;
System.out.println(“enter a,b,c values”);
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
p=a*b*c;
g=a>b&&a>c?a: Math.max(b, c);
for(i=g;i<=p;i=i+g){
if(i%a==0 &&i%b==0 && i%c==0){
break;
}
}
System.out.println(i);
s.close();
}
}
def lcm(num1, num2):
temp=num2
while True:
if(temp % num2 == 0 and temp % num1 == 0):
return temp
temp+=1
print(‘Enter three numbers: ‘)
a = int(input())
b = int(input())
c = int(input())
if(a<b):
two = lcm(a,b)
else:
two = lcm(b,a)
if(two<c):
result=lcm(two,c)
else:
result=lcm(c,two)
print('The LCM of three numbers is: ',result)
#Python
import math
def lcm(x,y,z):
lcm=x*y//math.gcd(x,y)
lcm=lcm*z//math.gcd(lcm,z)
return lcm
x=int(input())
y=int(input())
z=int(input())
print(lcm(x,y,z))
#in python
def lcm(x,y):
if x > y:
grt=x
else:
grt=y
while(True):
if((grt%x==0) and(grt%y==0)):
lcm=grt
break
grt+=1
return lcm
n= int(input())
m=int(input())
o=int(input())
if n<m:
l=lcm(n,m)
else:
l=lcm(m,n)
if l<o:
k=lcm(l,o)
else:
k=lcm(o,l)
print(k)