
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find LCM of Two Numbers in Java
In this article, we will learn to find the LCM of two numbers using Java. L.C.M. or Least Common Multiple of two values is the smallest positive value which is the multiple of both values.
For example, multiples of 3 and 4 are:
3 ? 3, 6, 9, 12, 15 ... 4 ? 4, 8, 12, 16, 20 ...
The smallest multiple of both is 12, hence the LCM of 3 and 4 is 12.
We will learn through two different methods to solve this: an incremental method and a formula-based approach using the Greatest Common Divisor (GCD).
Problem Statement
Write a program in Java to find the LCM of two numbers. Below is the demonstration of the same ?
Input
Enter first number ::
6
Enter second number ::
10
Output
LCM of given numbers is :: 1059
Different Approaches
Below are the different approaches to find the LCM of two numbers ?
Using incremental method
Following are the steps to find the LCM of two numbers using the incremental method ?
- First, import the Scanner class from java.util package to allow user input.
- Initialize integer variables a, b, max, step, and lcm.
- Instantiate the Scanner object to read input from the user.
- Ask the user to enter two integers, storing them in a and b.
- Determine Maximum: Identify the larger of the two numbers and assign it to max and step.
- Use a while loop to check for the smallest number that is divisible by both a and b:
- If max is divisible by both a and b, set lcm to max and exit the loop.
- If not, increment max by step and continue checking.
- Print the calculated LCM to the console and close the Scanner object to prevent resource leaks.
Example
Below is the Java program to find the LCM of two numbers ?
import java.util.Scanner; public class LCMOfTwoNumbers { public static void main(String args[]){ int a, b, max, step, lcm = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter first number ::"); a = sc.nextInt(); System.out.println("Enter second number ::"); b = sc.nextInt(); if(a > b){ max = step = a; } else{ max = step = b; } while(a!= 0) { if(max % a == 0 && max % b == 0) { lcm = max; break; } max += step; } System.out.println("LCM of given numbers is :: "+lcm); } }
Output
Enter first number :: 6 Enter second number :: 10 LCM of given numbers is :: 30
Using Math.gcd() method
Below is the Java program to calculate LCM using Math.gcd() ?
- Import the Scanner class from java.util package.
- Declare two integer variables, a and b, to store the user input values, and a variable lcm to store the result.
- Create a Scanner object to read the input from the user.
- Prompt the user to enter two numbers and assign the values to a and b.
- Calculate the LCM using the formula: LCM(a, b) = (a * b) / GCD(a, b), where GCD is obtained using the Math.gcd() method.
- Display the calculated LCM in the console.
- Close the program.
Example
Below is the Java program to find the LCM of two numbers ?
import java.util.Scanner; public class Main { public static void main(String args[]) { int a, b, lcm; Scanner sc = new Scanner(System.in); System.out.println("Enter first number ::"); a = sc.nextInt(); System.out.println("Enter second number ::"); b = sc.nextInt(); // Calculate LCM using the formula: LCM(a, b) = (a * b) / GCD(a, b) lcm = (a * b) / gcd(a, b); System.out.println("LCM of given numbers is :: " + lcm); } // Method to calculate GCD public static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } }
Output
Enter first number ::
33
Enter second number ::
44
LCM of given numbers is :: 132