Java program to find the LCM of two numbers

Java program to find the LCM of two numbers. The Least Common Multiple (LCM) of two numbers is the smallest positive integer that is evenly divisible by both numbers. One way to find the LCM of two numbers is by using the relationship between the GCD (Greatest Common Divisor) and LCM:

This method leverages the fact that the product of two numbers is equal to the product of their GCD and LCM.

Java Program to Find the LCM of Two Numbers

Java program to find the LCM of two numbers

Here’s a Java program that prompts the user to enter two numbers and then calculates their LCM using the relationship between GCD and LCM.

import java.util.Scanner;

public class LCM {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter two numbers
        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();
        
        System.out.print("Enter the second number: ");
        int num2 = scanner.nextInt();

        // Calculate the GCD of the two numbers
        int gcd = findGCD(num1, num2);

        // Calculate the LCM of the two numbers
        int lcm = (num1 * num2) / gcd;

        // Print the result
        System.out.println("The LCM of " + num1 + " and " + num2 + " is " + lcm + ".");
    }

    // Method to find the GCD using the Euclidean algorithm
    public static int findGCD(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}

Explanation of the Code (Java program to find the LCM of two numbers)

Let’s break down the program step-by-step to understand how it works.

  1. Importing the Scanner Class: We import the Scanner class from the java.util package to read input from the user.
import java.util.Scanner;

2. Creating the Main Class: We create a public class named LCM. In Java, the class name should match the filename.

public class LCM {

3. Main Method: The main method is the entry point of the program. Inside this method, we will write the code to find the LCM of two numbers.

public static void main(String[] args) {

4. Creating a Scanner Object: We create a Scanner object to read input from the user.

Scanner scanner = new Scanner(System.in);

5. Reading Input from the User: We prompt the user to enter two numbers and store these values in variables.

System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();

6. Calculating the GCD: We call the findGCD method to calculate the GCD of the two numbers. The result is stored in a variable gcd.

int gcd = findGCD(num1, num2);

7. Calculating the LCM: We use the relationship between GCD and LCM to calculate the LCM of the two numbers.

int lcm = (num1 * num2) / gcd;

8. Printing the Result: We print the LCM of the two numbers.

System.out.println("The LCM of " + num1 + " and " + num2 + " is " + lcm + ".");

9. Method to Find the GCD: The findGCD method uses the Euclidean algorithm to calculate the GCD of two numbers. The algorithm works by repeatedly replacing the larger number by its remainder when divided by the smaller number until the remainder is zero. The last non-zero remainder is the GCD.

public static int findGCD(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

Explanation of the findGCD Method

  1. Loop to Find GCD: The while loop continues as long as b is not zero. In each iteration, we update b to be the remainder of a divided by b, and update a to be the previous value of b.
while (b != 0) {
    int temp = b;
    b = a % b;
    a = temp;
}

2. Returning the GCD: When b becomes zero, a contains the GCD of the original two numbers. We return a.

return a;

Running the Program

To run the program, follow these steps:

  1. Save the code in a file named LCM.java.
  2. Open a command prompt or terminal and navigate to the directory where you saved the file.
  3. Compile the program using the following command:
javac LCM.java

4. Run the compiled program using the following command:

java LCM

5. Follow the prompt to enter two numbers. The program will display their LCM.

Example Output

If you run the program and enter 15 and 20 as the numbers, the output will be:

Enter the first number: 15
Enter the second number: 20
The LCM of 15 and 20 is 60.

What did we learn from this article?

Finding the LCM of two numbers is a fundamental exercise that helps in understanding arithmetic operations and the relationship between GCD and LCM in programming. This Java program demonstrates how to calculate the LCM using the Euclidean algorithm for GCD. By practicing such basic programs, you can strengthen your problem-solving skills and gain confidence in your ability to write efficient Java code. Keep practicing and exploring more problems to enhance your programming skills.

Top 100 Java Programs: Click Here

Are you looking for a job: Click Here

Leave a Comment