Java Program to Calculate LCM of two numbers

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

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

  1. The above program creates a lcmofTwo method:
    • Takes two integer parameters a and b.
    • Initializes the LCM variable lcm to the maximum of the two input numbers using Math.max(a, b).
    • Enters a while loop that continues indefinitely (while(true)).
    • Checks if lcm is divisible by both a and b without any remainder.
    • If lcm is divisible by both a and b, it breaks out of the loop.
    • If not, it increments lcm by 1 and continues to the next iteration.
    • Once the loop breaks, it returns the calculated LCM.
  2. In the main method:
    • It creates a Scanner object s to 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 lcmofTwo method with the two input numbers.
    • Prints the calculated LCM.
    • Closes the Scanner object to release system resources.

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

  1. The above java program imports the Scanner class to read input from the user.
  2. In the main method:
    • It creates a Scanner object sc to 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) and i (iterator) to 0 and 1, respectively.
    • Enters a while loop that continues until i is less than or equal to both num and num1.
    • Checks if both num and num1 are divisible by the current value of i without any remainder.
    • If they are, updates the hcf variable to store the current common factor.
    • Increments i for the next iteration.
  3. Calculates the LCM using the formula: LCM = (num * num1) / HCF.
    • Uses the formula lcm = (num * num1) / hcf to 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.

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

  1. The above java program imports the Scanner class to read input from the user.
  2. In the main method:
    • It creates a Scanner object sc to 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 lcm to the larger of the two input numbers.
    • Enters a for loop starting from lcm up to the product of the two numbers.
    • In each iteration, it checks if the current value of i is divisible by both num and num1.
    • If i is 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.

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.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.