Java Program to Find the LCM of Two Numbers

The easiest approach for finding the LCM is to Check the factors and then find the Union of all factors to get the result.

Below is the implementation of the above method:

Java
// Java Program to find 
// the LCM of two numbers
import java.io.*;

// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        // Numbers
        int a = 15, b = 25;

        // Checking for the largest
        // Number between them
        int ans = (a > b) ? a : b;

        // Checking for a smallest number that
        // can de divided by both numbers
        while (true) {
            if (ans % a == 0 && ans % b == 0)
                break;
            ans++;
        }

        // Printing the Result
        System.out.println("LCM of " + a + " and " + b
                           + " : " + ans);
    }
}

Output
LCM of 15 and 25 : 75




Java Program to Find LCM of Two Numbers

LCM (i.e. Least Common Multiple) is the largest of the two stated numbers that can be divided by both the given numbers. In this article, we will write a program to find the LCM in Java

Similar Reads

Java Program to Find the LCM of Two Numbers

The easiest approach for finding the LCM is to Check the factors and then find the Union of all factors to get the result....

Using Greatest Common Divisor

Below given formula for finding the LCM of two numbers ‘u’ and ‘v’ gives an efficient solution....

Contact Us