The GCD (Greatest Common Divisor), also called HCF (Highest Common Factor) , can be computed in C using ausing recursion in C programming and hence can make tasks easier in many situations.
C Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> int hcf(int n1, int n2); int main() { int n1, n2; printf("Enter first Number:"); scanf("%d", &n1); printf("Enter second Number:"); scanf("%d", &n2); printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1,n2)); return 0; } int gcd(int n1, int n2) { if (n2 != 0) return gcd(n2, n1%n2); else return n1; } |
Output: