As a Java programmer, you may come across situations where you need to find the least common multiple (LCM) of two numbers.
The LCM is the smallest multiple that is common to both numbers.
In this tutorial, we will show you how to write a Java program to find the LCM of two numbers.
The algorithm to find the LCM of two numbers is based on the fact that the product of two numbers is equal to the product of their GCD (greatest common divisor) and LCM.
So, to find the LCM of two numbers, we need to first find their GCD.
Here’s the Java program to find the LCM of two numbers:
import java.util.Scanner; public class LCM { public static void main(String[] args) { Scanner input = new Scanner(System.in); // read the input from the user System.out.print("Enter the first number: "); int num1 = input.nextInt(); System.out.print("Enter the second number: "); int num2 = input.nextInt(); // find the GCD of the two numbers int gcd = findGCD(num1, num2); // find the LCM using the formula LCM = (num1 * num2) / GCD int lcm = (num1 * num2) / gcd; // print the LCM System.out.println("The LCM of " + num1 + " and " + num2 + " is " + lcm); } // recursive method to find the GCD of two numbers public static int findGCD(int num1, int num2) { if (num2 == 0) { return num1; } return findGCD(num2, num1 % num2); } }
Let’s go through the code step-by-step:
- We first import the
Scanner
class from thejava.util
package to read input from the user. - In the
main
method, we prompt the user to enter the two numbers and read them using theScanner
object. - We then call the
findGCD
method to find the GCD of the two numbers. - Finally, we use the formula
LCM = (num1 * num2) / GCD
to find the LCM of the two numbers and print it to the console. - The
findGCD
method is a recursive method that uses the Euclidean algorithm to find the GCD of two numbers. - We then compile and run the program to test it.
That’s it! You now know how to write a Java program to find the LCM of two numbers using the GCD.
To test the program, we can input any two numbers and the program will output their LCM.
Here’s an example:
Enter the first number: 24 Enter the second number: 36 The LCM of 24 and 36 is 72
As you can see, the program correctly calculates the LCM of 24 and 36, which is 72.
In summary, the LCM of two numbers can be found using the formula LCM = (num1 * num2) / GCD, where GCD is the greatest common divisor of the two numbers.
To find the GCD, we can use the Euclidean algorithm, which is a recursive algorithm that finds the GCD of two numbers by repeatedly finding the remainder of the larger number divided by the smaller number.
In Java, we can implement this algorithm using a recursive method.
We prompt the user to enter the two numbers, find their GCD using the findGCD
method, and then use the formula to find their LCM. Finally, we print the LCM to the console.
I hope you found this tutorial helpful in understanding how to write a Java program to find the LCM of two numbers.
If you have any questions or suggestions, feel free to leave a comment below.