Home »
Java Programs »
Java Basic Programs
Java program to find the (LCM) Lowest Common Multiple
Given two numbers, we have to find the (LCM) Lowest Common Multiple.
Submitted by Nidhi, on February 26, 2022
Problem statement
In this program, we will read two integer numbers from the user and find the Lowest Common Multiple.
Source Code
The source code to find the LCM is given below. The given program is compiled and executed successfully.
// Java program to find the
// Lowest Common Multiple
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
int rem = 0;
int lcm = 0;
int X = 0;
int Y = 0;
System.out.printf("Enter Number1: ");
num1 = SC.nextInt();
System.out.printf("Enter Number2: ");
num2 = SC.nextInt();
if (num1 > num2) {
X = num1;
Y = num2;
} else {
X = num2;
Y = num1;
}
rem = X % Y;
while (rem != 0) {
X = Y;
Y = rem;
rem = X % Y;
}
lcm = num1 * num2 / Y;
System.out.printf("Lowest Common Multiple is: %d\n", lcm);
}
}
Output
Enter Number1: 10
Enter Number2: 225
Lowest Common Multiple is: 450
Explanation
In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().
The main() method is an entry point for the program. Here, we read two integer numbers from the user using the Scanner class. Then we calculated the Lowest Common Multiple (LCM) and printed the result.
Java Basic Programs »