Java Program To Find LCM of Two Numbers

We will discuss how to find LCM of Two Numbers In Java in this article . We have different methods to calculate lcm in java . We will discuss these method one by one . I will recommended to you must see Java Coding question for interview preparation.

What is LCM

LCM of two integers is the smallest positive integer that is perfectly divisible by both the numbers (without a remainder). It is also called least common multiple .

Example 1: LCM using while Loop and if Statement In Java

We will write a program to find lcm of two numbers in java using while loop and if statement in this example.

public class LCMUsingLoop {

	public static void main(String[] args) {

		int n1 = 40, n2 = 50, lcm;

		// maximum number from n1 and n2 is stored in lcm
		lcm = (n1 > n2) ? n1 : n2;

		// Always true
		while (true) {
			// check remainder must be zero
			if (lcm % n1 == 0 && lcm % n2 == 0) {
				System.out.println("LCM  :" + lcm + " of n1 : " + n1
						+ " and n2 : " + n2);
				break;
			}
			++lcm;
		}
	}

}

Output:

LCM  :200 of n1 : 40 and n2 : 50

We consider greater number from n1 and n2 is lcm now we divided lcm by n1 and n2 and reminder must be zero otherwise lcm increment by 1 , till lcm devided by n1 and n2.

Example 2: Calculate LCM using GCD In Java

We will write lcm program in java using hcf . Firstly We will find GCD of two number then calculate lcm using formula lcm = (n1*n2)/gcd .

public class LCMUsingGCD {
	public static void main(String[] args) {

		int n1 = 40, n2 = 50, gcd = 1;

		for (int i = 1; i <= n1 && i <= n2; ++i) {
			if (n1 % i == 0 && n2 % i == 0)
				gcd = i;
		}

		int lcm = (n1 * n2) / gcd;
		System.out.println("LCM  :" + lcm + " of n1 : " + n1 + " and n2 : "
				+ n2);
	}

}

Output :

LCM  :200 of n1 : 40 and n2 : 50

Example 3 : Find LCM Using Recursion Method

We will write java program for lcm of two numbers in java using recursion . We will read input using using scanner .

import java.util.Scanner;

public class LCMUsingRecursion {
	public static void main(String[] args) {
		int n1, n2, lcm;
		Scanner sc = new Scanner(System.in);
		System.out.println("enter  number 1");
		n1 = sc.nextInt();
		System.out.println("enter  number 2");
		n2 = sc.nextInt();

		lcm = n1 < n2 ? getLCM(n1, n2) : getLCM(n2, n1);
		System.out.println("LCM  :" + lcm + " of n1 : " + n1 + " and n2 : "
				+ n2);
	}

	static int lcm = 0;

	public static int getLCM(int n1, int n2) {

		// increase common by adding max value
		lcm += n2;

		if (lcm % n1 == 0)
			return lcm; // base case

		else
			return getLCM(n1, n2); // general case
	}

}

Output :

enter  number 1
20
enter  number 2
30
LCM  :60 of n1 : 20 and n2 : 30

In this lcm code in java we will add greater number in lcm and check base condition that it must be decided by small element otherwise it call recursively .We have seen how to find lcm of two numbers in java using scanner also in this example .

We have seen lcm code in java using different method . You can see more interesting java coding program for interview preparation .

Leave a Reply

Your email address will not be published. Required fields are marked *

47 − 39 =