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

 
 

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

	}
}
Advertisement

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


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 2, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com