🔄📚Intermediate
Calculate Power Using Recursion
Calculate x^n recursively
Calculate x^n using recursion: power(x, n) = x × power(x, n-1) with power(x, 0) = 1
Program Code
power_recursion.c
C
1#include <stdio.h>23long long power(int base, int exp) {4 if (exp == 0) return 1; // Base case5 if (exp < 0) return 0; // Handle negative6 return base * power(base, exp - 1);7}89int main() {10 int base, exp;11 12 printf("Enter base and exponent: ");13 scanf("%d %d", &base, &exp);14 15 printf("%d^%d = %lld\n", base, exp, power(base, exp));16 return 0;17}Output
Enter base and exponent: 2 10
2^10 = 1024
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials