Find L.C.M. of two numbers in Java


 

Find L.C.M. of two numbers in Java

In this section, you will learn how to find LCM(Least Common Element) of two integers using in Java.

In this section, you will learn how to find LCM(Least Common Element) of two integers using in Java.

Find L.C.M. of two numbers in Java Program

In this section, you will learn how to find LCM(Least Common Element) of two integers.The least common multiple is the smallest positive integer that is a multiple of both the numbers. Here we have created a method determineLCM(int a, int b) where we have compared the two variables and determine the lcm.

Here is the code:

public class FindLCM {
	public static int determineLCM(int a, int b) {
		int num1, num2;
		if (a > b) {
			num1 = a;
			num2 = b;
		} else {
			num1 = b;
			num2 = a;
		}
		for (int i = 1; i <= num2; i++) {
			if ((num1 * i) % num2 == 0) {
				return i * num1;
			}
		}
		throw new Error("Error");
	}

	public static void main(String[] args) {
		FindLCM lcm = new FindLCM();
		int d = lcm.determineLCM(6, 7);
		System.out.println(d);

	}
}

Ads