Java Program to Find GCD of two Numbers

Updated on November 28, 2024
Find gcd of two numbers header image

Introduction

The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them without leaving a remainder. In mathematics, it's a fundamental concept used for simplifying fractions and computing least common multiples. In programming, finding the GCD is a common algorithmic problem that can be solved using various methods, including the Euclidean algorithm.

In this article, you will learn how to write a Java program to find the GCD of two numbers. Discover how to implement this using the iterative and recursive versions of the Euclidean algorithm, which is not only efficient but also exemplifies a key technique in algorithm design.

Implementing GCD Using the Euclidean Algorithm

The Euclidean algorithm is an efficient method for computing the GCD of two integers. It is based on the principle that the GCD of two numbers also divides their difference.

Iterative Approach

  1. Start by checking if either number is zero. If one is, the GCD is the absolute value of the non-zero number.

  2. Continue subtracting the smaller number from the larger number until the two numbers become equal.

  3. Return the GCD, which is then the value of either number when they are equal.

    java
    public static int gcdIterative(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return Math.abs(a);
    }
    

    This code uses a while loop to repeatedly take the remainder of the two numbers until b becomes zero. At that point, a contains the GCD. The Math.abs() function ensures the GCD is a positive number.

Recursive Approach

  1. The recursive method has a simple base case: if b equals zero, return a.

  2. Else, call the method recursively, reducing the problem size each time (working with smaller numbers).

    java
    public static int gcdRecursive(int a, int b) {
        if (b == 0) {
            return Math.abs(a);
        }
        return gcdRecursive(b, a % b);
    }
    

    By using recursion, the solution leverages the stack to reduce the problem size on each recursive call until b equals zero. This version captures the elegance and simplicity of the Euclidean algorithm.

Sample Program Using GCD Function

To demonstrate the practical use of the GCD functions, consider a Java main method that uses both approaches to find the GCD of two numbers.

java
public class GCDExample {
    public static void main(String[] args) {
        int number1 = 36;
        int number2 = 60;

        System.out.println("Iterative GCD of " + number1 + " and " + number2 + " is: " + gcdIterative(number1, number2));
        System.out.println("Recursive GCD of " + number1 + " and " + number2 + " is: " + gcdRecursive(number1, number2));
    }
}

This simple Java program defines two integers and calculates their GCD using both the iterative and recursive method. It demonstrates how both functions are capable of returning the same result.

Conclusion

Understanding how to calculate the GCD of two numbers is a basic yet critical skill in both mathematics and software development. By implementing the Euclidean algorithm in Java, with both iterative and recursive approaches, you gain insight into not only the mechanics of the algorithm but also the practical aspects of recursion and iteration in programming. Apply these methods whenever you need to solve problems related to number theory or when you require the simplification of fractions in computational tasks.