- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes

- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages

- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation

- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples

# Java Program to Find LCM of two Numbers

In this article, we will understand how to calculate the LCM of two numbers in Java. Lowest Common Multiple (LCM) of two numbers is the smallest positive integer that is evenly divisible by both the numbers.

Below is a demonstration of the same −

**Input**

Suppose our input is −

24 and 18

**Output**

The desired output would be −

The LCM of the two numbers is 72

## Algorithm

Step1- Start Step 2- Declare three integers: input_1, inpur_2 and sum Step 3- Prompt the user to enter two integer value/ Hardcode the integer Step 4- Read the values Step 5- Using a while loop from 1 to the bigger number among the two inputs, check if the 'i'value divides both the inputs without leaving behind reminder. Step 6- Display the 'i' value as LCM of the two numbers Step 7- Stop

## Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .

import java.util.Scanner; public class LCM { public static void main(String[] args) { int input_1 , input_2 , lcm; Scanner scanner = new Scanner(System.in); System.out.println("A scanner object has been defined "); System.out.println("Enter the first number: "); input_1 = scanner.nextInt(); System.out.println("Enter the second number: "); input_2 = scanner.nextInt(); lcm = (input_1 > input_2) ? input_1 : input_2; while(true) { if( lcm % input_1 == 0 && lcm % input_2 == 0 ) { System.out.printf("The LCM of %d and %d is %d.", input_1, input_2, lcm); break; } ++lcm; } } }

## Output

A scanner object has been defined Enter the first number: 24 Enter the second number: 18 The LCM of 24 and 18 is 72.

## Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class LCM { public static void main(String[] args) { int input_1 , input_2 , lcm; input_1 = 24; input_2 = 18; System.out.println("The first number is " + input_1); System.out.println("The second number is " + input_2); lcm = (input_1 > input_2) ? input_1 : input_2; while(true) { if( lcm % input_1 == 0 && lcm % input_2 == 0 ) { System.out.printf("\nThe LCM of %d and %d is %d.", input_1, input_2, lcm); break; } ++lcm; } } }

## Output

The first number is 24 The second number is 18 The LCM of 24 and 18 is 72.

- Related Articles
- Java program to find the LCM of two numbers
- Swift Program to Find LCM of two Numbers
- Haskell program to find lcm of two numbers
- Kotlin Program to Find LCM of two Numbers
- Program to find LCM of two Fibonnaci Numbers in C++
- Find LCM of two numbers
- GCD and LCM of two numbers in Java
- Java Program to Find GCD of two Numbers
- C++ Program to Find the GCD and LCM of n Numbers
- Java program to find the GCD or HCF of two numbers
- Java Program to Find the Product of Two Numbers Using Recursion
- How to find the LCM of two given numbers using Recursion in Golang?
- Java Program to Add Two Numbers
- Java Program to Swap Two Numbers.
- The product of two numbers is 1944 and their LCM is 108. Find the HCF of the two numbers.

Advertisements