calculate Least Common Multiple lcm of two number in java

h‮tt‬ps://www.lautturi.com
calculate Least Common Multiple lcm of two number in java
/**
 * @author lautturi.com 
 * Java example: calculate Least Common Multiple of two numbers in java
 */

import java.util.*;

public class Lautturi {
	public static void main(String[] args) {
		 int n1 = 6, n2 = 4, gcd = 1;

		    for(int i = 1; i <= n1 && i <= n2; ++i) {
		      if(n1 % i == 0 && n2 % i == 0)
		        gcd = i;
		    }

		    int lcm = (n1 * n2) / gcd;
		    System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
	}
}

output:

The LCM of 6 and 4 is 12.
Created Time:2017-10-03 15:55:09  Author:lautturi