Home Tutorial Java Core Find L.C.M. of two numbers in Java

 
 

Find L.C.M. of two numbers in Java
Posted on: November 2, 2009 at 12:00 AM
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);

	}
}

Related Tags for Find L.C.M. of two numbers in Java:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.