GCD using Recursion in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>

int gcd(int a, int b) {
    if(b == 0) return a;
    return gcd(b, a % b);
}

int main() {
    int a = 48, b = 18;
    printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));
    return 0;
}

C Output

Input:
48 18

Output:
GCD of 48 and 18 is 6


C++ Program

#include <iostream>
using namespace std;

int gcd(int a, int b) {
    if(b == 0) return a;
    return gcd(b, a % b);
}

int main() {
    int a = 56, b = 98;
    cout << "GCD of " << a << " and " << b << " is " << gcd(a, b);
    return 0;
}

C++ Output

Input:
56 98

Output:
GCD of 56 and 98 is 14


JAVA Program

public class Main {
    static int gcd(int a, int b) {
        if(b == 0) return a;
        return gcd(b, a % b);
    }
    public static void main(String[] args) {
        int a = 81, b = 27;
        System.out.println("GCD of " + a + " and " + b + " is " + gcd(a, b));
    }
}

JAVA Output

Input:
81 27

Output:
GCD of 81 and 27 is 27


Python Program

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

a, b = 20, 30
print(f"GCD of {a} and {b} is {gcd(a, b)}")

Python Output

Input:
20 30

Output:
GCD of 20 and 30 is 10


Explanation
Example
The Greatest Common Divisor of two numbers is the greatest number that divides both with a remainder of zero. For instance, for 48 and 18, their common divisors are 1, 2, 3, 6, and the greatest is 6. The recursive approach is founded on the Euclidean Algorithm, which defines that gcd(a, b) = gcd(b, a % b) and terminates when b is 0. This is because the GCD of two numbers equals the GCD of the smaller number and the remainder when the larger is divided by the smaller.

Real-Life Analogy
Consider cutting two ropes so that both are divided into equal pieces of the biggest possible length. You continue to trim the longer rope by the length of the shorter rope until one rope length is 0. The remaining rope length is the biggest size that exactly measures both ropes — that's the GCD. 

Why It Matters
The GCD is also extensively used in fraction simplification, cryptography protocols such as RSA, and number theory problems. The study of the recursive GCD educates on how mathematical properties can be utilized to transform a problem significantly. The approach reduces the size of the problem rapidly and elegantly without attempting all possible divisors.

Complexity and Optimization
Time complexity of the Euclidean Algorithm is O(log min(a, b)), which is significantly better than iterating over divisors from top to bottom (O(min(a, b))). Space complexity is O(log min(a, b)) due to the call stack in recursion. Thus, it is a fine example of recursion with both efficiency and elegance.

Edge Cases and Testing
If one of them is 0, the other number is the GCD. If both are 0, the GCD is undefined but most programs return 0. Negative inputs should either be normalized to positive prior to calculation or treated with absolute values since GCD is usually defined for non-negative integers.

Interview Tips
Interviewers will usually request the GCD implementation because it is brief but facilitates in-depth discussion of recursion, efficiency, and mathematical proof. Having written the simple recursive version, they could request the iterative solution, or they might ask you to extend it to calculate the LCM (Least Common Multiple) using the formula LCM(a, b) = (a × b) / GCD(a, b).

SEO-Friendly Closing
The C implementation of the GCD using recursion in C, C++, Java, and Python is a must-have for developers, with mathematical beauty coupled with algorithmic effectiveness. With this, beginners discover how recursion is applied to solve practical problems such as fraction reduction, modular number calculations, and generating cryptography keys. This algorithm is very efficient, is commonly taught in computer science classes, and is regularly seen in coding job interviews and competitive programming, so it is a part of any programmer's toolkit that must be known.