GCD (Recursive) in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

/* C - GCD using recursion (Euclidean Algorithm) */
#include <stdio.h>

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

int main() {
    int x, y;
    if (scanf("%d %d", &x, &y) != 2) return 0;
    printf("%d\n", gcd(x, y));
    return 0;
}

C Output

Input:
48 18

Output:
6



C++ Program

// C++ - GCD using recursion
#include <iostream>
using namespace std;

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

int main() {
    int x, y;
    if (!(cin >> x >> y)) return 0;
    cout << gcd(x, y) << "\n";
    return 0;
}

C++ Output

Input:
54 24

Output:
6



JAVA Program

// Java - GCD using recursion
import java.util.*;
public class Main {
    static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        if (!sc.hasNextInt()) return;
        int x = sc.nextInt(), y = sc.nextInt();
        System.out.println(gcd(x, y));
    }
}

JAVA Output

Input:
81 153

Output:
9



Python Program

# Python - GCD using recursion
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)

x, y = map(int, input().split())
print(gcd(x, y))

Python Output

Input:
101 103

Output:
1



In-Depth Learning – Entire Concept in Paragraphs
Example:
Greatest common divisor (GCD) of two integers is the highest integer that can evenly divide both integers. The recursive approach employed in this example emulates the Euclidean algorithm, which is founded on the fact that GCD(a, b) = GCD(b, a % b) and that the process terminates once b becomes zero, leaving a to be the GCD. This works because any number that divides both a and b also divides their difference (or remainder). The recursion keeps reducing the problem into smaller pairs until it reaches the base case.

Real-Life Analogy:
Consider two runners beginning at the same point but running at different rates around a circular path. At some point, they will meet once again at the starting point after running a certain amount of distance. That "meeting distance" is similar to the GCD — the greatest distance that divides evenly into both runners' total distance without remainder.

Why It Matters:
GCD is an important mathematical and computer science tool. It's crucial for reducing fractions to lowest terms, manipulating ratios, doing modular arithmetic, and in such cryptographic schemes as RSA. The recursive Euclidean algorithm is not only mathematically beautiful but also computationally efficient with a time complexity of O(log(min(a, b))).

Learning Insights:
This exercise reiterates important principles of recursion — the existence of a base case (b == 0) and a recursive step that gets closer to that base case. It's also a nice illustration of how mathematical properties yield elegant, efficient code. Learning about GCD also naturally results in learning about the Least Common Multiple (LCM), because LCM(a, b) = (a * b) / GCD(a, b).

Interview and Project Relevance:
GCD is used as an interview warm-up question since it checks for recursion, modulus, and mathematical thought. Some variations can involve calculating the GCD of more than two integers, applying it to fraction simplification, or calculating it for extremely large integers where performance is critical. In project work, GCD logic comes up in computational geometry (reducing slopes), number theory usage, and algorithm optimization based on step sizes or cycles.

SEO-friendly summary:
This is a comprehensive tutorial on the recursive GCD technique and presents neat, concise C, C++, Java, and Python codes for the Euclidean algorithm. Each of these programs finds the greatest common divisor of two integers effectively via recursion with a detailed description of how and why it works. Learning the recursive GCD not just enhances your understanding of recursion but also prepares you for interviews and actual programming problems where number theory and performance tuning meet.