2:00 am - Tuesday April 23, 2019

Java Example Program to calculate LCM

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
//Example Program to calculate LCM of two numbers
/**
 * @author: candidjava.com
 * @description: calculate the LCM of two given numbers
 */
public class LCM {
	public static void main(String[] args) {
		int a = 36;
		int b = 92;
		int big;
		int small;
		// print the two numbers for which the LCM to be calculate
		System.out.println("the value of a:" + a);

		System.out.println("the value of b:" + b);
		{
			if (a > b) // check whether a is big or b is big
			{
				big = a;
				small = b;
			} else {
				big = b;
				small = a;
			}
			for (int i = 1; i <= big; i++) {
				if (((big * i) % small) == 0) {
					int lcm = big * i; // condition to calculate the LCM of two
										// numbers
					System.out.println("The least common multiple is " + (lcm));
					// print the LCM of two numbers
					break;
				}
			}
		}
	}
}

Output:
the value of a:36
the value of b:92
The least common multiple is 828

Filed in: Z-A All Java Codes

Comments are closed.