THINK FIRST·CODE LATER

← All labs

gcd and lcm

Problem

Write a class MathTools with two static methods:

public static int gcd(int a, int b)
public static int lcm(int a, int b)

gcd returns the greatest common divisor (use Euclid's method: while b is not 0, replace the pair (a, b) by (b, a % b)), and lcm returns the least common multiple, computed as a / gcd(a, b) * b.

The main method reads two positive integers and prints:

GCD: 6
LCM: 36

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.