LCM of Three Numbers C Program asked in Wipro NLTH
July 4, 2023
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))
#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;
}
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.
Disclaimer-: The questions provided on this page are only model practice questions there is no surety that these questions have been previously asked in any company placement papers, these questions here only have the sole purpose to make you practice coding questions
#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
#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)