Tuesday, May 8, 2012

GCD implementation in Java


public class Maths {

      public int gcd(int p, int q) {
            if (q == 0){
                  return p;
            }
            return gcd(q, p % q);
      }

      public static void main(String[] args) {
            int a = 36;
            int b = 48;
           
            Maths m = new Maths();
            int g = m.gcd(a,b);
            System.out.println("GCD is " + g);
      }
}

No comments:

Post a Comment