Kodekraftt
/Blog
/Lcm Of Two Numbers In Java With Explanation Formula Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

LCM of Two Numbers in Java with Explanation, Formula, Examples, and Program

The LCM of two numbers is the smallest number that is perfectly divisible by both. This article explains the concept with examples and provides a simple and efficient Java program.

LCM of Two Numbers in Java

The LCM (Least Common Multiple) of two numbers is the smallest positive integer that is divisible by both the numbers. A very efficient way to calculate the LCM is by using the GCD (Greatest Common Divisor).

Formula

LCM(a, b) = (a × b) / GCD(a, b)

This formula works because the product of two numbers is always equal to the product of their GCD and LCM.


Example

Find LCM of 12 and 18

  • GCD(12, 18) = 6
  • LCM = (12 × 18) / 6
  • LCM = 216 / 6 = 36

So, the LCM of 12 and 18 is 36.


Java Program to Find LCM


public class LCM {

    // Function to calculate GCD using Euclidean Algorithm
    static int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    // Function to calculate LCM using the formula
    static int lcm(int a, int b) {
        return (a * b) / gcd(a, b);
    }

    public static void main(String[] args) {
        int num1 = 12;
        int num2 = 18;

        int result = lcm(num1, num2);
        System.out.println("LCM of " + num1 + " and " + num2 + " is: " + result);
    }
}

Practice Challenges

  1. Write a program to find the LCM of three numbers.
  2. Print all numbers between 1 and 100 whose LCM with 15 is less than 200.
  3. Modify the program to display both GCD and LCM along with the steps used to calculate them.

You might also like

Perfect Square in Java with Explanation, Definition, Examples, and Program

A Perfect Square is a number that can be expressed as the square of an integer. This guide explains the concept with examples and provides a simple Java program to check if a number is a perfect square.

Check Perfect Square in Java Without Using Math.sqrt() – Explanation, Logic, Examples, and Program

Learn how to check whether a number is a Perfect Square in Java without using Math.sqrt(). This guide explains the logic, provides examples, and includes an efficient program using iterative checking.

Find Square Root of a Number in Java Without Using Math.sqrt() – Explanation, Logic, Examples, and Program

Learn how to calculate the square root of any number in Java without using Math.sqrt(). This guide explains the binary search method, includes examples, and provides a clean Java program.

Perfect Square in Java with Explanation, Definition, Examples, and Program

A Perfect Square is a number that can be expressed as the square of an integer. This guide explains the concept with examples and provides a simple Java program to check if a number is a perfect square.

Perfect Cube in Java with Explanation, Definition, Examples, and Program

A Perfect Cube is a number that can be expressed as the cube of an integer. This guide explains the concept with examples and provides a simple Java program to check if a number is a perfect cube.

100 Java Programs for Beginners with Examples and Solutions

Explore 100 Java programs for beginners with explanations, logic, and examples. Learn Java step by step through number programs, string programs, pattern problems, mathematical logic, and practical coding exercises.