Euclidean Algorithm In Java
Chapter:
Math Class
Last Updated:
05-05-2016 18:47:26 UTC
Program:
/* ............... START ............... */
public class JavaEuclideanAlgorithm {
// find greatest common divisor
public static int gcd(int a, int b) {
int r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
public static void main(String[] args) {
System.out.println("Common Divisors:");
System.out.println(" 5 7 : " + gcd(5, 7));
System.out.println(" 99 6 : " + gcd(99, 6));
System.out.println(" 100 10 : " + gcd(100, 10));
System.out.println(" 990 77 : " + gcd(990, 77));
}
}
/* ............... END ............... */
Output
Common Divisors:
5 7 : 1
99 6 : 3
100 10 : 10
990 77 : 11
Tags
Euclidean Algorithm, Java, Math