Greatest Common Divisor
Java code that gets greatest common divisor of two given ints
// (c) by Taatudi public class gcd { public static void main(String[] args) { if (args.length != 2) { System.out.println("usage: gcd <int> <int>"); System.exit(1); } int a, b; int t2 = Integer.parseInt(args[0]); int t1 = Integer.parseInt(args[1]); if(t1 > t2) { a = t1; b = t2; } else { b = t1; a = t2; } int m = a % b; while (m != 0) { a = b; b = m; m = a % b; } System.out.println("gcd: "+ b); } }

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Download this code in plain text format here