Java递归


Java递归

import java.util.Scanner;

public class GCDTest {

   public static void main(String[] args) {

      int g1,g2;

      System.out.println("enter two numbers");

      Scanner sc=new Scanner(System.in);

      g1 = sc.nextInt();

      g2 = sc.nextInt();

      int gcd = gcd(g1, g2);

      System.out.printf("G.C.D of %d and %d is %d.", g1, g2, gcd);

   }

   public static int gcd(int g1, int g2)

   {

      if (g2 != 0)

         return gcd(g2, g1 % g2);

      else

         return g1;

   }

}