Java Program to Calculate the LCM of Two Numbers
In this example, we are giving a program to calculate the LCM(Lowest common multiple) of two numbers.
To calculate the LCM:
# To calculate the LCM
public class LCM {
public static void main(String[] args) {
int n1 = 12, n2 = 15, lcm;
lcm = (n1 > n2) ? n1 : n2;
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;
}
}
}
Output:
G.C.D of 49 and 7 is 7