Find GCD of two numbers in java using recursion

Let’s learn to find GCD of two numbers in java using recursion.

Find GCD of two numbers in java using recursion

Here’s an example to find GCD of two numbers using recursion.

public class GCDUsingRecursion
{
   public static void main(String[] args) 
   {
      int number1 = 898, number2 = 90;
      int gcd = gcdRecursion(number1, number2);
      System.out.println("G.C.D of " + number1 + " and " + number2 + " is " + gcd);
   }
   public static int gcdRecursion(int num1, int num2)
   {
      if(num2 != 0)
      {
         return gcdRecursion(num2, num1 % num2);
      }
      else
      {
         return num1;
      }
   }
}


Output:

G.C.D of 898 and 90 is 2


Also read – methods in java