// // GCDtest.java // // Class GCDtest implements the Euclidean algorithm for finding the // greatest common divisor: int gcd(int x, int y) // The program takes two integer arguments from the command line x and y // and returns the gcd(x,y) class GCDtest { private final static String USAGE = "Usage: java GCDtest x y"; public static void main(String[] args) { // test number of input arguments if (args.length != 2) { System.err.println(USAGE); System.exit(-1); } try { // read in command line input int x = java.lang.Integer.parseInt(args[0]); int y = java.lang.Integer.parseInt(args[1]); // calculate and print gcd recusively System.out.println(gcd(x,y)); } catch (NumberFormatException ex) { System.out.println(USAGE); System.exit(-1); } } // recursive algorithm to calculate the greatest common divisor of two integers private static int gcd(int x, int y) { if (y != 0) return gcd (y, x % y); else return java.lang.Math.abs(x); } }