Introduction
The Least Common Multiple (LCM) of two numbers is the smallest positive integer that is divisible by both numbers. The LCM is often used in problems related to fractions, scheduling, and number theory. This guide will walk you through writing a Java program that calculates the LCM of two given numbers.
Problem Statement
Create a Java program that:
- Prompts the user to enter two integers.
- Calculates the LCM of the two integers.
- Displays the LCM.
Example:
- Input:
12
and15
- Output:
"The LCM of 12 and 15 is 60"
Solution Steps
- Read the Two Numbers: Use the
Scanner
class to take the two integers as input from the user. - Calculate the GCD: Implement the Euclidean algorithm to find the Greatest Common Divisor (GCD) of the two numbers.
- Calculate the LCM: Use the relationship between GCD and LCM:
[
\text{LCM} = \frac{\text{num1} \times \text{num2}}{\text{GCD}}
] - Display the LCM: Print the calculated LCM.
Java Program
// Java Program to Find the LCM of Two Numbers // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class LCMCalculator { public static void main(String[] args) { // Step 1: Read the two numbers from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); // Step 2: Calculate the GCD int gcd = findGCD(num1, num2); // Step 3: Calculate the LCM int lcm = (num1 * num2) / gcd; // Step 4: Display the LCM 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
Step 1: Read the Two Numbers
- The
Scanner
class is used to read two integer inputs from the user. ThenextInt()
method captures each number.
Step 2: Calculate the GCD
- The
findGCD()
method implements the Euclidean algorithm to calculate the GCD of two numbers. - The Euclidean algorithm finds the GCD by repeatedly replacing the larger number with the remainder of dividing the larger number by the smaller number until the remainder is 0.
Step 3: Calculate the LCM
- The LCM is calculated using the formula:
[
\text{LCM} = \frac{\text{num1} \times \text{num2}}{\text{GCD}}
] - This formula leverages the relationship between GCD and LCM, ensuring that the LCM is the smallest number that is divisible by both input numbers.
Step 4: Display the LCM
- The program prints the LCM of the two numbers using
System.out.println()
.
Output Example
Example:
Enter the first number: 12 Enter the second number: 15 The LCM of 12 and 15 is 60
Conclusion
This Java program demonstrates how to calculate and display the LCM of two given numbers using the relationship between GCD and LCM. It covers essential concepts such as loops, arithmetic operations, and user input handling, making it a valuable exercise for beginners learning Java programming.