SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

LCM Program in Java and Python

20 October 2012

LCM program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to find the Least Common Multiple (L.C.M.) of two numbers entered by the user.

Note: This is the first Method of finding the L.C.M. of two numbers. Method 2 of finding the HCF can be read from here [Finding LCM - Method 2] .

INPUT: Enter the first number: 336 Enter the second number: 224 OUTPUT: L.C.M. = 672

Algorithm:

Step 1: Start.

Step 2: Accept two numbers a and b.

Step 3: Initialize candidate to the greater of a and b.

Step 4: Initialize limit to a * b because the product is always a common multiple.

Step 5: Repeat while candidate is less than or equal to limit.

Step 6: Check whether candidate % a is 0 and candidate % b is 0.

Step 7: If both remainders are 0, store candidate as LCM and stop the loop.

Step 8: If not, increment candidate and test the next number.

Step 9: Display the stored LCM.

Step 10: Stop.

Explanation:

The LCM is the smallest number that is exactly divisible by both given numbers. This program searches for that number by testing possible multiples.

The search starts from one of the given numbers and continues up to the product of the two numbers. The product is always a common multiple, so the answer must be found by that point.

For each value, the program checks whether it is divisible by both numbers using the modulus operator. If both remainders are 0, the value is a common multiple.

Because the search moves upward, the first common multiple found is the least common multiple. The loop can stop immediately after printing it.

The loop tests candidate multiples in increasing order. The first candidate whose remainders with both numbers are zero is stored as the answer and the search stops immediately.

The candidate variable behaves like a counter through possible answers. Each loop iteration compares the candidate with both numbers through remainder checks, and the stored result is returned only when both checks pass.

The LCM is the smallest number that is exactly divisible by both input numbers. A simple method starts from the larger of the two numbers and tests multiples until one is divisible by both. Another method uses the relationship between product, HCF and LCM. In this program, the loop or formula is designed to find the first common multiple, not merely any common multiple. The stopping condition is important because the first valid value encountered in increasing order is the least common multiple.

Java Program:

Java
/**
* The class LcmMethod_1 takes 2 numbers as Input and finds their LCM.
* This is Method 1
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;
class LcmMethod_1
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int a,b,lcm=1;
        System.out.print("Enter the 1st number : ");
        a=sc.nextInt();
        System.out.print("Enter the 2nd number : ");
        b=sc.nextInt();
        for(int i=a;i<=a*b;i++) //Even if you start the for loop by 1, you will get the answer, but starting it from either the first or the second number reduces the number of times the for loop is executed.
        {
            if(i%a==0 && i%b==0) //Checking the first number which is divisible by both the numbers
            {
                lcm=i;
                break; //exiting from the loop, as we don’t need anymore checking after getting the LCM
            }
        }
        System.out.println("L.C.M. = "+lcm);
    }
}

Equivalent Python Program:

Python
# Read the number and keep any required copy for digit or divisor processing.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))

lcm = 1

for i in range(a, a * b + 1):
    if i % a == 0 and i % b == 0:
        lcm = i
        break

print("L.C.M. =", lcm)

Output:

INPUT: Enter the first number: 336 Enter the second number: 224 OUTPUT: L.C.M. = 672

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →