LCM of Three Numbers
How to Find LCM of three numbers
NOTE:- Please comment down the code in other languages as well below –
C
C++
Java
Python
C
Run
#includeusing namespace std; int findGCD(int a, int b) { if (b == 0) return a; return findGCD(b, a % b); } int findLCM(int a, int b, int c) { int gcd = findGCD(a, b); int lcm = (a * b) / gcd; gcd = findGCD(lcm, c); lcm = (lcm * c) / gcd; return lcm; } int main() { int num1, num2, num3; cout << "Enter the first number: "; cin >> num1; cout << "Enter the second number: "; cin >> num2; cout << "Enter the third number: "; cin >> num3; int lcm = findLCM(num1, num2, num3); cout << "LCM: " << lcm << endl; return 0; }
C++
Run
#includeusing namespace std; int findGCD(int a, int b) { if (b == 0) return a; return findGCD(b, a % b); } int findLCM(int a, int b, int c) { int gcd = findGCD(a, b); int lcm = (a * b) / gcd; gcd = findGCD(lcm, c); lcm = (lcm * c) / gcd; return lcm; } int main() { int num1, num2, num3; cout << "Enter the first number: "; cin >> num1; cout << "Enter the second number: "; cin >> num2; cout << "Enter the third number: "; cin >> num3; int lcm = findLCM(num1, num2, num3); cout << "LCM: " << lcm << endl; return 0; }
Java
Run
import java.util.Scanner; public class Main { public static int findGCD(int a, int b) { if (b == 0) return a; return findGCD(b, a % b); } public static int findLCM(int a, int b, int c) { int gcd = findGCD(a, b); int lcm = (a * b) / gcd; gcd = findGCD(lcm, c); lcm = (lcm * c) / gcd; return lcm; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); System.out.print("Enter the third number: "); int num3 = scanner.nextInt(); scanner.close(); int lcm = findLCM(num1, num2, num3); System.out.println("LCM: " + lcm); } } }
Python
Run
def find_gcd(a, b): while b: a, b = b, a % b return a def find_lcm(a, b, c): gcd = find_gcd(a, b) lcm = (a * b) // gcd gcd = find_gcd(lcm, c) lcm = (lcm * c) // gcd return lcm num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) num3 = int(input("Enter the third number: ")) lcm = find_lcm(num1, num2, num3) print("LCM:", lcm)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int a = sc.nextInt();
int gcd =1;
for(int i = 2; i<=n && i<=m ; i++){
if(n%i==0 && m%i==0 ){
gcd = i;
}
}
int Lc =(n*m)/gcd;
for(int i =2; i<=Lc && i<=a; i++){
if(Lc%i==0 && a%i==0){
gcd=i;
}
}
int Lcm =(Lc*a)/gcd;
System.out.println(Lcm);
}
}
Join our Discord Community for your technical doubts
#include
int main(){
int num1, num2;
printf(“Input your number 1:”);
scanf(“%d”, &num1);
printf(“Input your number 2:”);
scanf(“%d”, &num2);
//find max and main
int max = num1, min = num2;
if(num1 < num2){
max = num2;
min = num1;
}
int k = 1, LCM;
do{
LCM = max*k;
k++;
}while(LCM%min == 0);
printf("LCM of %d and %d is %d", num1 ,num2, LCM);
}
class CalculateLCM
{ public static void main(String s[])
{
int result = calculateLcm(5, 10, 15);
System.out.println(“The Lowest Common Multiple is ” + result);
}
public static int calculateLcm(int first, int second, int third)
{
int result = 0;
//Write a code here to calculate the LCM of three numbers and assign it to result.
return result;
}
}
n=int(input(“n”))
m=int(input(“m”))
p=int(input(“p”))
if(m>n and m>p):
lg=m
elif(n>p and n>m):
lg=n
else:
lg=p
while(True):
if(lg%n==0 and lg%m==0 and lg%p==0):
print(lg)
break
lg+=1
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
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
#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
#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;
}
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;
}
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
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)