In java, to calculate LCM (Least Common Multiple) of two numbers we can use HCF or Brute Force Method or Multiples of Numbers. LCM also known as Lowest Common Multiple, Least Common Denominator (as it used while adding, subtracting of fractions, we find the lcm of denominators).
When user enters two input values, we need to give the least common multiple of those two numbers. In this article we will learn through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java operators
- Loop Concepts
Problem Statement
We have to write a java program to print Least common multiple of two numbers. Our program should take two input integer values and should print smallest common multiple of those two numbers. This action will be done in our program. Let’s see an example,
Example 1:
For input integer numbers 9 and 12, Output is 36.
Explanation:
Input integer number num = 9
Input integer number num1 = 12
Multiples of 9: 9,18,27,36,45,54,63,72,81,90.....
Multiples of 12: 12,24,36,48,60,72,84,96,108,120....
Common multiples of 9 and 12: 36 and 72
Least/Smallest common multiple is 36
Hence, 36 is the least common multiple of 9 and 12.
Or we can use HCF to get LCM of two numbers. Let’s see an example,
Example 2:
For the input integer numbers 4 and 8, Output should be 8.
Explanation:
Input integer number num = 4
Input integer number num1 = 8
Factors of 4 = 1,2,4
Factors of 8 = 1,2,4,8
hcf = 4
lcm(num,num1)= a*b/hcf(num,num1) = lcm = 4*8/4 = 8
Hence, 8 is the least common multiple of 4 and 8.
Logic to print LCM of two numbers
There are multiple logics such as using hcf to get lcm, Brute Force approach and multiples of numbers. Brute Force approach and multiples of numbers (Both use same concept of iterating through large number but the way we do is different) are not used regularly as they are lengthy and executes multiple number of times and it gives time complexity.
Logic 1: Using Brute Force Approach
- For given two integer numbers, find the maximum and keep condition to true so that it will executes continuously.
- In the loop, checks if maximum number is divisible by both integer numbers without any remainder.
- If maximum number is divisible by both integer numbers, it breaks out of the loop.
- If not, it increments maximum number by 1 and continues to the next iteration. And gives the least common multiple of those two input numbers.
Logic 2: Using HCF (Refer this in Print HCF article)
- After finding hcf, find the lcm using hcf
- lcm=num*num1/hcf(num,num1)
- Print the result.
Logic 3: Using multiples of number
- For two input integer numbers, find the maximum number using ternary operator and checks if maximum number is divisible by both integer numbers without any remainder.
- If maximum number is divisible by both integer numbers, it breaks out of the loop.
- If not, it increments maximum number by 1 and continues to the next iteration. And gives the least common multiple of those two input numbers.
Java program to print LCM of two numbers
Program 1: Using Brute Force Approach
In the below program we are using brute force approach to print the lcm of two numbers.
import java.util.Scanner;
public class LcmOfTwoNumbers {
public static int lcmofTwo(int a, int b) {
int lcm = Math.max(a, b); //find max
while(true) { // Executes always,many times execution so gives time complexity. Not much efficient
if(lcm%a==0 && lcm%b==0) {
break;
}
lcm++;
}
return lcm;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter first number");
int num = s.nextInt();
System.out.println("Enter second number");
int num1 = s.nextInt();
int result = lcmofTwo(num,num1);
System.out.println("Lcm of two numbers are : "+result);
s.close();
}
}Output:
Enter first number
2
Enter second number
18
Lcm of two numbers are : 18
Java program explanation to print LCM of two numbers
- The above program creates a
lcmofTwomethod:- Takes two integer parameters
aandb. - Initializes the LCM variable
lcmto the maximum of the two input numbers usingMath.max(a, b). - Enters a
whileloop that continues indefinitely (while(true)). - Checks if
lcmis divisible by bothaandbwithout any remainder. - If
lcmis divisible by bothaandb, it breaks out of the loop. - If not, it increments
lcmby 1 and continues to the next iteration. - Once the loop breaks, it returns the calculated LCM.
- Takes two integer parameters
- In the
mainmethod:- It creates a Scanner object
sto read input from the user. - Asks the user to enter the first number.
- Reads the first number entered by the user.
- Prompts the user to enter the second number.
- Reads the second number entered by the user.
- Calls the
lcmofTwomethod with the two input numbers. - Prints the calculated LCM.
- Closes the Scanner object to release system resources.
- It creates a Scanner object
Program 2: Using HCF
In the below program we are using HCF to print the lcm of two numbers.
import java.util.Scanner;
public class LcmUsingGCDOfTwo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
int num = sc.nextInt();
System.out.println("Enter second number");
int num1 = sc.nextInt();
int hcf=0;
int i=1;
while(i<=num || i<=num1) {
if(num%i==0 && num1%i==0)
hcf=i;
i++;
}
int lcm = (num*num1)/hcf; //lcm(a,b)*gcd(a,b)=a*b
// lcm9a,b) =a*b/gcd(a,b)
System.out.println("LCM of given two numbers are: "+lcm);
sc.close();
}
}Output:
Enter first number
9
Enter second number
21
LCM of given two numbers are: 63
Java program explanation to print LCM of two numbers
- The above java program imports the Scanner class to read input from the user.
- In the
mainmethod:- It creates a Scanner object
scto read input from the user. - Asks the user to enter the first number.
- Reads the first number entered by the user.
- Prompts the user to enter the second number.
- Reads the second number entered by the user.
- Initializes the variables
hcf(highest common factor) andi(iterator) to 0 and 1, respectively. - Enters a
whileloop that continues untiliis less than or equal to bothnumandnum1. - Checks if both
numandnum1are divisible by the current value ofiwithout any remainder. - If they are, updates the
hcfvariable to store the current common factor. - Increments
ifor the next iteration.
- It creates a Scanner object
- Calculates the LCM using the formula: LCM = (num * num1) / HCF.
- Uses the formula
lcm = (num * num1) / hcfto calculate the LCM. - Utilizes the relationship between LCM and GCD: LCM * GCD = num * num1.
- Prints the calculated LCM.
- Closes the Scanner object to release system resources.
- Uses the formula
Program 3: Using multiples of numbers
In the below program we are using multiples of numbers to print the lcm of two numbers.
import java.util.Scanner;
public class LcmOfTwo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
int num = sc.nextInt();
System.out.println("Enter second number");
int num1 = sc.nextInt();
int i;
int lcm =(num>num1)? num:num1;
for( i=lcm;i<=num*num1;i++) {
if(i%num==0 && i%num1==0) {
break;
}
}
System.out.println("LCM of two numbers are: "+i);
sc.close();
}
}Output:
Enter first number
34
Enter second number
56
LCM of two numbers are: 952
Java program explanation to print LCM of two numbers
- The above java program imports the Scanner class to read input from the user.
- In the
mainmethod:- It creates a Scanner object
scto read input from the user. - Asks the user to enter the first number.
- Reads the first number entered by the user.
- Prompts the user to enter the second number.
- Reads the second number entered by the user.
- Initializes the variable
lcmto the larger of the two input numbers. - Enters a
forloop starting fromlcmup to the product of the two numbers. - In each iteration, it checks if the current value of
iis divisible by bothnumandnum1. - If
iis divisible by both numbers without any remainder, it breaks out of the loop. - Prints the value of
i, which represents the LCM of the two input numbers. - Closes the Scanner object to release system resources.
- It creates a Scanner object
Conclusion
The programs we did here gives us least common multiple of two numbers using Brute Force method, Using HCF and using multiples of numbers.

