Examples
- Leap Year Program in Java (Check Leap Year or Not)
- Check Number is Positive or Negative in Java (4 Ways)
- Java Program to Check Character is Alphabet or Not
- Armstrong Number Program in Java (for loop, Recursion)
- Print Prime Numbers Between 1 to N in Java (1 to 100)
- Java Program for Palindrome Number (Palindrome Code)
- Sum of n Natural Numbers in Java (Programs & Explanation)
- Java Multiplication Table Program (Loops, 2D Array) 5 Ways
- Find GCD of Two Numbers in Java (HCF Program)
- GCD of Three Numbers in Java (HCF of 3 Numbers Program
- GCD of Array in Java (GCD of n Numbers Program)
- LCM of Two Numbers in Java (LCM Program and Code)
- LCM of Three Numbers in Java (Easy Programs)
- LCM of n Numbers in Java (LCM of Array of Numbers)
- How to Print A to Z in Java? 3 Ways to Print Alphabets
LCM of n Numbers in Java (LCM of Array of Numbers)
In computer programming, efficiency and versatility are key. When working with a set of numbers, there often arises a need to determine their Least Common Multiple (LCM).
Whether you're an experienced Java developer or just beginning your journey in programming, understanding how to find the LCM of an array of numbers in Java is an invaluable skill.
The LCM is a fundamental mathematical concept that finds application in various domains of computer science and mathematics. It represents the smallest multiple that is evenly divisible by a set of integers. In practical terms, LCM is frequently used in tasks involving scheduling, fractions, and simplifying mathematical operations.
Throughout this tutorial, we will explore various methods and strategies to calculate the LCM of n numbers in Java, ranging from basic approaches to more optimized solutions.
Concepts to Learn:
Java Program for LCM of Array of Numbers
Here's how to find the LCM of an array of numbers in Java using the first method, which involves using the GCD and math operations:
Code
import java.util.Scanner;
public class LCMExample {
// Function to find the GCD of two numbers
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// Function to find the LCM of an array of numbers
public static int findLCM(int[] arr) {
int lcm = arr[0];
for (int i = 1; i < arr.length; i++) {
int currentNumber = arr[i];
lcm = (lcm * currentNumber) / gcd(lcm, currentNumber);
}
return lcm;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input array of numbers
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
// Find and display the LCM
int result = findLCM(arr);
System.out.println("LCM of the array is: " + result);
}
}
Output
Enter the number of elements in the array: 3
Enter the elements of the array:
12 18 24
LCM of the array is: 72
Explanation
-
We define a gcd function to calculate the GCD of two numbers using the Euclidean algorithm.
-
The findLCM function takes an array of numbers as input and calculates the LCM by iterating through the array. It initializes the LCM with the first number and then, in each iteration, updates the LCM using the formula (lcm * currentNumber) / gcd(lcm, currentNumber).
-
In the main method, we take input for the array of numbers, and then we call the findLCM function to calculate the LCM of the array.
LCM of n Numbers in Java Using Iteration
Here's how to find the LCM of n numbers in Java using the second method, which involves iteration:
Code
import java.util.Scanner;
public class LCMExample {
// Function to find the LCM of two numbers
public static int findLCM(int a, int b) {
int max = Math.max(a, b);
int lcm = max;
while (true) {
if (lcm % a == 0 && lcm % b == 0) {
return lcm;
}
lcm += max;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the number of elements
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
// Input the numbers
int[] numbers = new int[n];
System.out.println("Enter the numbers:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}
// Calculate the LCM iteratively
int lcm = numbers[0];
for (int i = 1; i < n; i++) {
lcm = findLCM(lcm, numbers[i]);
}
// Display the LCM
System.out.println("LCM of the numbers is: " + lcm);
}
}
Output
Enter the number of elements: 3
Enter the numbers:
6 8 12
LCM of the numbers is: 24
Explanation
In this method, we first define a findLCM function that calculates the LCM of two numbers a and b using a loop.
It starts with the larger of the two numbers as the potential LCM (lcm). It then increments lcm by the value of the larger number in each iteration until it finds a number that is divisible by both a and b.
In the main method, we take input for the number of elements and the array of numbers.
We calculate the LCM iteratively by initializing lcm with the first number and then finding the LCM of lcm and each subsequent number in the array.
LCM of Array in Java Using Formula
Here's how to find the LCM of n numbers in Java using the third method, which involves using the LCM formula:
Code
import java.util.Scanner;
public class LCMExample {
// Function to find the LCM of two numbers
public static int findLCM(int a, int b) {
return (a * b) / gcd(a, b);
}
// Function to find the GCD of two numbers
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the number of elements
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
// Input the numbers
int[] numbers = new int[n];
System.out.println("Enter the numbers:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}
// Calculate the LCM using the formula
int lcm = numbers[0];
for (int i = 1; i < n; i++) {
lcm = findLCM(lcm, numbers[i]);
}
// Display the LCM
System.out.println("LCM of the numbers is: " + lcm);
}
}
Output
Enter the number of elements: 3
Enter the numbers:
4 6 8
LCM of the numbers is: 24
Explanation
In this method, we define two functions: findLCM and gcd. The findLCM function calculates the LCM of two numbers a and b using the LCM formula, which is (a * b) / gcd(a, b). The gcd function calculates the GCD using the Euclidean algorithm.
In the main method, we take input for the number of elements and the array of numbers.
We calculate the LCM iteratively by initializing lcm with the first number and then finding the LCM of lcm and each subsequent number in the array using the findLCM function.
Learn Next: