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 18Output:
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 24Output:
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 153Output:
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 103Output:
1
In-Depth Learning – Entire Concept in Paragraphs
Social Plugin