Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, November 2, 2020

Finding LCM In 2 Ways in Java With Simple Example Programs

1. Overview

In this short article, you'll learn how to find the LCM of two numbers in java by using simple while loop and by using GCD of same numbers.

It is easy to understand if you know the below topics.

If else condition

while loop

LCM means least common multiple - In other words a minimum number that is divisible by given two numbers without any remainder.

Example

Input : a = 12, b = 40

Multiple factors are : 2, 2, 3, 10

LCM is : 2 * 2 * 3 * 10 = 120

2. Example To Find LCM Using While Loop in java

In the below example, first declared two int variables a, b.

Next, take the highest value from a and b as lcm.

Finally, run the while loop until both numbers are divided by the same number.

public class LCMWhileLoopExample {

	public static void main(String[] args) {

		// creating a two integer variables
		int a = 100;
		int b = 152;

		// Example 1
		printLCM(a, b);

		// Example 2
		a = 100;
		b = 200;

		printLCM(a, b);

		// Example 3
		a = 82;
		b = 205;

		printLCM(a, b);

	}

	private static void printLCM(int a, int b) {
		// taking big number as lcm default.

		int lcm = b;
		if (a > b) {
			lcm = a;
		}

		while (true) {

			if (lcm % a == 0 && lcm % b == 0) {
				System.out.printf("\nThe LCM of two numbers %d and %d is %d.", a, b, lcm);
				break;
			}

			lcm++;
		}

	}

}

Output:

The LCM of two numbers 100 and 152 is 3800.
The LCM of two numbers 100 and 200 is 200.
The LCM of two numbers 82 and 205 is 410.

3. Example To Find LCM Using GCD of two numbers in java

This is the different approach and takes less time for computing the LCM.

But, first need to calculate the GCD of two numbers.

GCD means Greatest Common Divisor means highest number that is divided by two numbers without any remainder.

If you do not know GCD, you can learn how to find GCD of two numbers?

After finding the GCD, use the below formula to get LCM.

LCM = a * b / GCD.

public class LCMFromGCDExample {

	public static void main(String[] args) {

		printLCMUsingGCD(100, 152);
		printLCMUsingGCD(10, 1100);
		printLCMUsingGCD(140, 300);
	
	}

	private static void printLCMUsingGCD(int a, int b) {
		
		// finding GCD
		int gcd = 1;
		for (int i = 1; i <= a && i <= b; i++) {
			
			if(a % i == 0 && b % i == 0) {
				gcd = i;
			}
		}

		int lcm = a * b / gcd;
		System.out.printf("\nThe LCM of two numbers %d and %d is %d.", a, b, lcm);

	}

}

Output

The LCM of two numbers 100 and 152 is 3800.
The LCM of two numbers 10 and 1100 is 1100.
The LCM of two numbers 140 and 300 is 2100.

4. Conclusion

In this article, you've seen how to calculate the LCM of two numbers using while loop and  by using GCD method.

GitHub LCM While Loop

GitHub LCM using GCD

In next article, How to find the LCM for large numbers ?

No comments:

Post a Comment

Please do not add any spam links in the comments section.