Euclid's Greatest Common Divisor (GCD) - Java Language Basics

Java examples for Language Basics:Operator

Description

Euclid's Greatest Common Divisor (GCD)

Demo Code

public class Main {
    public static void main(String[] arg) {
        int a = 32;
        int b = 24;
        System.out.println("Computing GCD(" + a + "," + b + ")");

        while (a != b) {
            if (a > b) a = a - b;
            else b = b - a;
        }/*from  ww w .  j  a v a  2s  .  c o  m*/
        // Display to console
        System.out.println("Greatest common divisor is " + a);
    }
}

Result


Related Tutorials