Java Program To Find LCM Of Two Numbers
Chapter:
Miscellaneous
Last Updated:
29-08-2017 14:53:33 UTC
Program:
/* ............... START ............... */
public class JavaLCMOfTwoNumbers {
public static void main(String[] args) {
int n1 = 72, n2 = 120, lcm;
lcm = (n1 > n2) ? n1 : n2;
// Always true
while (true) {
if (lcm % n1 == 0 && lcm % n2 == 0) {
System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
break;
}
++lcm;
}
}
}
/* ............... END ............... */
Output
The LCM of 72 and 120 is 360.
Notes:
-
The LCM of two integers is the smallest positive integer that is perfectly divisible by both the numbers.
Tags
Find LCM Of Two Numbers, Java, Miscellaneous