Find GCD of three numbers in java

Let’s learn to find GCD of three numbers in java.

Find GCD of three numbers in java

GCD is largest positive integer that can exactly divide each one of the number.

Here’s an example on to find GCD of three numbers.

import java.util.Scanner;
public class GCDOfThreeNumbers 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      int number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      int number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      int number3 = sc.nextInt();
      int gcdNumbers = GCDOfThreeNumbers.findGCD(number1, number2, number3);
      System.out.println("GCD of three numbers " + number1 + ", " + number2 + " and " + number3 + " is: " + gcdNumbers);
      sc.close();
   }
   public static int findGCD(int x, int y) 
   {
      if(y == 0)
      {
         return x;
      }
      else
      {
         return findGCD(y, x % y);
      }
   }
   public static int findGCD(int x, int y, int z) 
   {
      return findGCD(findGCD(x, y), z);
   }
}


Output:

Please enter first number: 12
Please enter second number: 48
Please enter third number: 84
GCD of three numbers 12, 48 and 84 is: 12


Also read – variables in java