Monday, September 22, 2025

Greatest Common Divisor (GCD) of Two Numbers Java Program

In this post we'll see how to write a Java program to calculate the greatest common divisor (GCD) of two numbers. It is also known as the highest common factor (HCF).

GCD of two integers is the highest integer that can completely divide both the integers which means remainder is 0 for both of the integers. For example- GCD of 33 and 110 is 11 as that is the highest number which completely divides both numbers.

33 = [1, 3, 11]
110 = [1, 2, 5, 10, 11]

Java program to calculate GCD of two numbers can be written using following approaches-

  1. Writing the logic as per the explanation of calculating GCD. Find the highest number that completely divided both numbers by doing that in a loop.
  2. Using Euclid algorithm (Repeated subtraction logic)
  3. Using Euclid algorithm (Repeated division logic)

1. Finding highest common divisor using loop

In this approach you need to run the loop from 1 up to minimum of two numbers and keep dividing both the numbers with the current loop iteration value. If it completely divides both the numbers then store it. By the end of the loop you will have the GCD.

import java.util.Scanner;
public class GCDCompute {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    System.out.println("Enter first number");
    int a = sc.nextInt();
    
    System.out.println("Enter second number");
    int b = sc.nextInt();

    System.out.println(computeGCD(a, b));      
  }
  
  public static int computeGCD(int a, int b) {
    // edge case where one or both numbers are zero
    if (a == 0) {
        return b;
    }
    if (b == 0) {
        return a;
    }
    int gcd = 1;
    for(int i = 1; i <= a && i <= b; i++) {
      if(a%i == 0 && b%i==0) {
        // completely divides so store the i value
        gcd = i;
      }
    }
    return gcd;
  }
}

Output

Enter first number
112
Enter second number
30
GCD is 2
Enter first number
0
Enter second number
5
GCD is 5

2. Using Euclid algorithm (Repeated subtraction logic) - Java Program

Euclid algorithm is considered more efficient than the general approach shown above. The repeated subtraction variant of Euclid algorithm states that the GCD of two numbers remains same if the greatest of the two numbers is replaced by the difference of the two numbers. This process is repeatedly done until both of the numbers become equal. This number at the end is the GCD of the two given numbers.

For example, if the numbers are 36 and 16.

  1. Greater of the two is 36 so that is replaced by the difference of the two.
  2. So, now the numbers are 20 and 16 again process is repeated
  3. Numbers are now 16 and 4; again subtract 4 from 16 giving 12
  4. Numbers 12 and 4 give 8 and 4
  5. 8 and 4 give 4 and 4, since numbers are equal now so 4 is the GCD.
public class GCDCompute {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter first number");
    int a = sc.nextInt();
    
    System.out.println("Enter second number");
    int b = sc.nextInt();

    System.out.println("GCD is " + gcdEuclidSubtraction (a, b));
  }
  
  public static int gcdEuclidSubtraction(int a, int b) {
    // edge case where one or both numbers are zero
    if (a == 0) {
        return b;
    }
    if (b == 0) {
        return a;
    }
    while(a != b) {
      if(a > b) {
        a = a - b;
      }else {
        b = b - a;
      }
    }
    
    return a;
  }
}

Output

Enter first number
36
Enter second number
16
GCD is 4

3. Using Euclid algorithm (Repeated division logic) - Java Program

This variant of Euclid algorithm further optimizes it by using division rather than subtraction. It works as follows.

  1. Divide the greater of the two number by the second number.
  2. Assign second number to the first number and remainder to the second number.
  3. Repeat step 1 and 2 until the remainder is zero. The last non-zero remainder is the GCD.

For example, if the two numbers are 36 and 16 then-

  1. Divide 36 by 16 giving remainder as 4.
  2. Now 16 becomes the first number and 4 becomes the second number. Repeat the process of division giving remainder as 0.
  3. That last non-zero remainder 4 is the GCD.
public class GCDCompute {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter first number");
    int a = sc.nextInt();
    
    System.out.println("Enter second number");
    int b = sc.nextInt();

    System.out.println("GCD is " + gcdEuclidDivision(a, b));    
  }
  public static int gcdEuclidDivision(int a, int b) {
    int temp;
    // Find the maximum of two numbers, which is assigned to variable a
    if(a < b) {      
      temp = a;
      a = b;
      b = temp;
    }
    while(b != 0) {
      temp = b;
      b = a % b;
      a = temp;
    }
    return a;
  }
}

Output

Enter first number
440
Enter second number
550
GCD is 110
Enter first number
17
Enter second number
37
GCD is 1

That's all for this topic Greatest Common Divisor (GCD) of Two Numbers Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Convert int to String in Java
  2. Convert float to int in Java
  3. Arrange Non-Negative Integers to Form Largest Number - Java Program
  4. How to Convert Date to String in Java
  5. Java String Interview Questions And Answers

You may also like-

  1. Matrix Multiplication Java Program
  2. Reading Delimited File in Java Using Scanner
  3. How to Untar a File in Java
  4. static Block in Java
  5. Is String Thread Safe in Java
  6. Try-With-Resources in Java With Examples
  7. Java ThreadLocal Class With Examples
  8. AtomicLong in Java With Examples

No comments:

Post a Comment