/** * @author lautturi.com * Java example: calculate Least Common Multiple in java */ import java.util.*; public class Lautturi { public static void main(String[] args) { int n1 = 4, n2 = 6, 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:
The LCM of 4 and 6 is 12.