🔄📚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>
2
3long long power(int base, int exp) {
4 if (exp == 0) return 1; // Base case
5 if (exp < 0) return 0; // Handle negative
6 return base * power(base, exp - 1);
7}
8
9int 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