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 Three Numbers in Java (Easy Programs)
When it comes to numbers, one fundamental concept is the Least Common Multiple (LCM), which helps us identify the smallest multiple shared by two or more numbers. LCM plays a vital role in various mathematical and computational applications. Understanding how to calculate LCM of three numbers in Java is an essential skill for any programmer.
This tutorial is designed to help you learn the process of finding the LCM of three numbers in Java. This guide will walk you through the steps, provide practical code examples, and offer insights into the underlying mathematical principles.
Concepts to Learn:
LCM of Three Numbers in Java Using GCD
Here's an example of finding the LCM of three numbers in Java using the first method, which involves utilizing the Greatest Common Divisor (GCD):
Code
import java.util.Scanner;
public class LCMCalculator {
// Function to calculate the GCD of two numbers using Euclidean algorithm
static int calculateGCD(int a, int b) {
if (b == 0) {
return a;
}
return calculateGCD(b, a % b);
}
// Function to calculate the LCM of three numbers using GCD
static int calculateLCM(int a, int b, int c) {
int gcdAB = calculateGCD(a, b);
int gcdABC = calculateGCD(gcdAB, c);
return (a * b * c) / gcdABC;
}
public static void main(String[] args) {
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();
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();
int lcm = calculateLCM(num1, num2, num3);
System.out.println("The LCM of " + num1 + ", " + num2 + ", and " + num3 + " is: " + lcm);
}
}
Output
Enter the first number: 12
Enter the second number: 18
Enter the third number: 24
The LCM of 12, 18, and 24 is: 72
Explanation
In this Java program, we first calculate the GCD of two numbers using the Euclidean algorithm in the calculateGCD method. Then, we find the GCD of the result obtained from the first GCD calculation and the third number, effectively finding the GCD of all three numbers.
To find the LCM, we use the formula: LCM(a, b, c) = (a * b * c) / GCD(a, b, c). This formula is applied in the calculateLCM method, which takes three numbers as input and returns their LCM.
In the example provided, we input three numbers (12, 18, and 24), and the program calculates their LCM, which is 72.
Java Program for LCM of Three Numbers Using Brute-Force Iteration
Here's an example of finding the LCM of 3 numbers in Java using the second method, which involves brute-force iteration:
Code
import java.util.Scanner;
public class LCMCalculator {
// Function to calculate the LCM of three numbers using brute-force iteration
static int calculateLCM(int a, int b, int c) {
int max = Math.max(a, Math.max(b, c)); // Find the maximum of three numbers
int lcm = max;
while (true) {
if (lcm % a == 0 && lcm % b == 0 && lcm % c == 0) {
return lcm;
}
lcm += max;
}
}
public static void main(String[] args) {
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();
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();
int lcm = calculateLCM(num1, num2, num3);
System.out.println("The LCM of " + num1 + ", " + num2 + ", and " + num3 + " is: " + lcm);
}
}
Output
Enter the first number: 12
Enter the second number: 18
Enter the third number: 24
The LCM of 12, 18, and 24 is: 72
Explanation
In this Java program, we calculate the LCM of 3 numbers using a brute-force iteration approach. We first find the maximum of the three numbers using Math.max. We initialize a variable lcm with the value of the maximum number because the LCM must be at least as large as the largest number.
We then enter a while loop that continues indefinitely until we find the LCM. Inside the loop, we check if lcm is divisible by all three numbers (a, b, and c) without leaving a remainder. If it is, we have found the LCM, and we return it. If not, we increment lcm by the value of the maximum number, as the LCM must be a multiple of the largest number.
LCM of 3 Numbers in Java Using while loop
Here's an example of finding the LCM of three numbers in Java using the third method, which involves while loop and prime factorization:
Code
import java.util.Scanner;
public class LCMCalculator {
// Function to calculate the LCM of two numbers using prime factorization
static int calculateLCM(int a, int b, int c) {
int lcm = 1;
int divisor = 2;
while (a > 1 || b > 1 || c > 1) {
if (a % divisor == 0 || b % divisor == 0 || c % divisor == 0) {
lcm *= divisor;
if (a % divisor == 0) {
a /= divisor;
}
if (b % divisor == 0) {
b /= divisor;
}
if (c % divisor == 0) {
c /= divisor;
}
} else {
divisor++;
}
}
return lcm;
}
public static void main(String[] args) {
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();
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();
int lcm = calculateLCM(num1, num2, num3);
System.out.println("The LCM of " + num1 + ", " + num2 + ", and " + num3 + " is: " + lcm);
}
}
Output
Enter the first number: 12
Enter the second number: 18
Enter the third number: 24
The LCM of 12, 18, and 24 is: 72
Explanation
In this Java program, we calculate the LCM of 3 numbers using the prime factorization method. The idea behind this method is to find the prime factors of each number and then multiply them to obtain the LCM.
We start with an initial LCM value of 1 and a divisor of 2. We then enter a while loop that continues until all three numbers (a, b, and c) become 1, indicating that we have factored out all prime factors.
Inside the loop, we check if the divisor is a factor of any of the three numbers. If it is, we multiply the LCM by the divisor and divide the respective number by the divisor to reduce it further. If the divisor is not a factor, we increment the divisor.
This process continues until all three numbers become 1. At this point, the LCM will contain all the prime factors of the original three numbers.
Learn Next: