Calculate GCD, greatest common divisor - Java Algorithm

Java examples for Algorithm:Number

Description

Calculate GCD, greatest common divisor

Demo Code

public class Main {
  public static int gcdCalcultion(int n1, int n2) {
    while (n2 > 0) {
      int r = n1 % n2;
      n1 = n2;/*  w  w  w.  j a v a  2s  .  c  o  m*/
      n2 = r;
    }

    return n1;
  }

  public static void main(String args[]) throws Exception {

    int reult = gcdCalcultion(12, 24);
    System.out.print("GCD: " + reult);
  }
}

Related Tutorials