In this tutorial, we are going to learn java program to calculate the least common multiple of two numbers.
Our program will take two numbers as the input from the user and return the LCM of input numbers.
For example
if the user inputs the numbers 4 and 6. then the output should be ‘12’ as 12 is the LCM of 4 and 6.
Program 1: Calculate LCM of two numbers in Java
In the below program we are following the traditional approach by taking two numbers as input from the user and iteratively checking for the LCM. We are using while loop to iterate and increments the maximum of the two numbers until it finds a number that is evenly divisible by both. The output for the input numbers 54 and 4 was correctly calculated as 108.
import java.util.*;
public class Main
{
public static void main(String[] args) {
double num1,num2,maxNum;
System.out.println("Java Program to calculate LCM" );
Scanner sc = new Scanner(System.in);
System.out.println("Please give first number");
num1= sc.nextDouble();
System.out.println("Please give second number");
num2 = sc.nextDouble();
maxNum = (num1 > num2) ? num1 : num2;
while (true) {
if (maxNum % num1 == 0 && maxNum % num2 == 0) {
System.out.println("LCM = "+maxNum );
break;
}
++maxNum;
}
}
}
Output
Java Program to calculate LCM
Please give first number
54
Please give second number
4
LCM = 108.0
Program 2: Calculate LCM of two numbers using Java 8
In the below program we are using Java 8 streams and functional programming concepts to calculate LCM. The calculateLCM
method used to calculate GCD by calling calculateGCD
method. And then applying the LCM formula. The output for the input numbers 54 and 4 was again correctly computed as 108.
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please give first number:");
int num1 = scanner.nextInt();
System.out.println("Please give second number:");
int num2 = scanner.nextInt();
int lcm = calculateLCM(num1, num2);
System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);
}
private static int calculateLCM(int num1, int num2) {
int gcd = calculateGCD(num1, num2);
return (num1 * num2) / gcd;
}
private static int calculateGCD(int a, int b) {
return IntStream.rangeClosed(1, Math.min(a, b))
.filter(i -> a % i == 0 && b % i == 0)
.max()
.orElse(1);
}
}
Output
Please give first number:
54
Please give second number:
4
LCM of 54 and 4 is: 108
Conclusion
In the above tutorial we have seen how to calculate the Least Common Multiple (LCM) of two numbers using Java. LCM represents the smallest common multiple that is divisible by both input numbers.
In the above program we have seen both logics building as well using Java 8 stream api concepts.
You are now equipped with two different methods to calculate the LCM of two numbers in Java. Whether using a traditional iterative approach or leveraging the functional capabilities of Java 8 streams, you have the flexibility to choose the method that best fits your coding preferences and requirements.
Happy coding!
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…