Java Program to Find GCD of two Numbers

To understand this example, you should have the knowledge of the following Java programming topics:


The HCF or GCD of two integers is the largest integer that can exactly divide both numbers (without a remainder).

Example 1: Find GCD of two numbers using for loop and if statement

class Main {
  public static void main(String[] args) {

    // find GCD between n1 and n2
    int n1 = 81, n2 = 153;
    
    // initially set to gcd
    int gcd = 1;

    for (int i = 1; i <= n1 && i <= n2; ++i) {

      // check if i perfectly divides both n1 and n2
      if (n1 % i == 0 && n2 % i == 0)
        gcd = i;
    }

    System.out.println("GCD of " + n1 +" and " + n2 + " is " + gcd);
  }
}

Output

GCD of 81 and 153 is 9

Here, two numbers whose GCD are to be found are stored in n1 and n2 respectively.

Then, a for loop is executed until i is less than both n1 and n2. This way, all numbers between 1 and smallest of the two numbers are iterated to find the GCD.

If both n1 and n2 are divisble by i, gcd is set to the number. This goes on until it finds the largest number (GCD) which divides both n1 and n2 without remainder.


We can also solve this problem using a while loop as follows:

Example 2: Find GCD of two numbers using while loop and if else statement

class Main {
  public static void main(String[] args) {

    // find GCD between n1 and n2
    int n1 = 81, n2 = 153;
    
    while(n1 != n2) {
    
      if(n1 > n2) {
        n1 -= n2;
      }
      
      else {
        n2 -= n1;
      }
    }

    System.out.println("GCD: " + n1);
  }
}

Output

GCD: 9

This is a better way to find the GCD. In this method, smaller integer is subtracted from the larger integer, and the result is assigned to the variable holding larger integer. This process is continued until n1 and n2 are equal.

The above two programs works as intended only if the user enters positive integers. Here's a little modification of the second example to find the GCD for both positive and negative integers.


Example 3: GCD for both positive and negative numbers

class GCD {
  public static void main(String[] args) {

    int n1 = 81, n2 = -153;

    // Always set to positive
    n1 = ( n1 > 0) ? n1 : -n1;
    n2 = ( n2 > 0) ? n2 : -n2;

    while(n1 != n2) {
        
      if(n1 > n2) {
        n1 -= n2;
      }
      
      else {
        n2 -= n1;
      }
    }
    
    System.out.println("GCD: " + n1);
  }
}

Output

GCD: 9

Also Read:

Before we wrap up, let’s put your knowledge of Java Program to Find GCD of two Numbers to the test! Can you solve the following challenge?

Challenge:

Write a function to calculate HCF of two numbers.

  • The HCF (Highest Common Factor) is the largest number that divides two numbers without leaving a remainder.
  • For example, the HCF of 12 and 15 is 3, as 3 is the largest number that divides both 12 and 15 without leaving a remainder.
  • Return the HCF of num1 and num2.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges