---
title: Find g.c.d using recursion
url: https://docs.vultr.com/java/examples/find-g-c-d-using-recursion
publish_date: 2024-12-04T15:20:00.089933Z
last_updated: 2024-12-04T15:20:00.089933Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/1244/c5ee2555-7333-48f7-b7c2-2e97fcbadadd.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Find G.C.D Using Recursion

## Introduction
The Greatest Common Divisor (GCD), also known as the greatest common factor or highest common factor, is the highest number that divides two integers without leaving a remainder. In programming, calculating the GCD is a common problem that can be solved using various methods, including recursion. Recursion is a powerful concept where a method calls itself to solve a problem.

In this article, you will learn how to implement a Java program to find the GCD of two numbers using recursion. Explore how this recursive approach works through detailed examples and understand how to integrate this method into your Java applications.

## Java Recursive Method to Find GCD

### Understanding the Recursive GCD Algorithm
The recursive method for finding the GCD of two numbers relies on the Euclidean algorithm, which is based on the principle that the GCD of two numbers also divides their difference.

Here is the basic concept:
1. If `y` is 0, then `gcd(x, y)` is `x`; otherwise,
1. `gcd(x, y)` is `gcd(y, x % y)`.

### Implementing the Recursive GCD Method

1. Create a new Java class named `GCD`.
1. Define the `gcd` method that takes two integer parameters.
1. Implement the recursive logic within this method.

    ```java
    public class GCD {
        public static int gcd(int x, int y) {
            if (y == 0) {
                return x;
            } else {
                return gcd(y, x % y);
            }
        }
    }
    ```

    In this code snippet, the `gcd` method checks if `y` is zero. If so, it returns `x` as the GCD. If not, it recursively calls itself with `y` and the remainder of `x` divided by `y`.

### Testing the GCD Method

1. Add a `main` method to the `GCD` class to test the `gcd` function.
1. Include test cases to verify the correctness of the method.

    ```java
    public static void main(String[] args) {
        int num1 = 36;
        int num2 = 60;
        System.out.println("G.C.D of " + num1 + " and " + num2 + " is " + gcd(num1, num2));
    }
    ```

    This code tests the `gcd` method by finding the GCD of 36 and 60. When you run this program, it should display "G.C.D of 36 and 60 is 12".

## Conclusion
Utilizing recursion to find the GCD of two numbers in Java is an effective and efficient approach. The recursive method is clean, straightforward, and reduces the complexity of the code. By following the steps outlined, you can implement this recursive GCD calculation in your own Java projects. The Euclidean algorithm's recursive form not only simplifies the problem but also teaches the fundamental concept of recursion in programming.