C Program to Calculate the Power Using Recursion

Calculating the power of a number is a common mathematical operation, and using recursion to perform this calculation is an elegant solution. In this article, we will explore three different C Program to Calculate the Power Using Recursion. Each solution will include a detailed explanation, example code, and output.

Prerequisites

Before diving into the solutions, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with functions and recursion.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Basic Recursion

1.1 Explanation

In this approach, we will use a simple recursive function to calculate the power of a number. The base case for the recursion is when the exponent is 0, which returns 1. For other cases, the function calls itself with the exponent decremented by 1.

1.2 Program

C
#include <stdio.h>

int power(int base, int exp) {
    if (exp == 0) {
        return 1;
    } else {
        return base * power(base, exp - 1);
    }
}

int main() {
    int base, exp;
    printf("Enter base: ");
    scanf("%d", &base);
    printf("Enter exponent: ");
    scanf("%d", &exp);

    int result = power(base, exp);
    printf("%d^%d = %d\n", base, exp, result);

    return 0;
}

1.3 Output

C
Enter base: 2
Enter exponent: 3
2^3 = 8

Solution 2: Optimized Recursion (Exponentiation by Squaring)

2.1 Explanation

In this solution, we will use an optimized recursive approach called exponentiation by squaring. This method reduces the number of multiplications required by dividing the problem into smaller subproblems.

2.2 Program

C
#include <stdio.h>

int power(int base, int exp) {
    if (exp == 0) {
        return 1;
    } else if (exp % 2 == 0) {
        int halfPower = power(base, exp / 2);
        return halfPower * halfPower;
    } else {
        return base * power(base, exp - 1);
    }
}

int main() {
    int base, exp;
    printf("Enter base: ");
    scanf("%d", &base);
    printf("Enter exponent: ");
    scanf("%d", &exp);

    int result = power(base, exp);
    printf("%d^%d = %d\n", base, exp, result);

    return 0;
}

Output

C
Enter base: 3
Enter exponent: 4
3^4 = 81

Solution 3: Tail Recursion

3.1 Explanation

Tail recursion is an optimized form of recursion where the recursive call is the last operation in the function. In this solution, we will implement a tail-recursive function to calculate the power of a number.

3.2 Program

C
#include <stdio.h>

int powerTailRec(int base, int exp, int result) {
    if (exp == 0) {
        return result;
    } else {
        return powerTailRec(base, exp - 1, result * base);
    }
}

int power(int base, int exp) {
    return powerTailRec(base, exp, 1);
}

int main() {
    int base, exp;
    printf("Enter base: ");
    scanf("%d", &base);
    printf("Enter exponent: ");
    scanf("%d", &exp);

    int result = power(base, exp);
    printf("%d^%d = %d\n", base, exp, result);

    return 0;
}

Output

C
Enter base: 5
Enter exponent: 3
5^3 = 125

Conclusion

Calculating the power of a number using recursion in C can be done in various ways, each suitable for different scenarios. The basic recursive approach is straightforward but may not be efficient for large exponents. The optimized recursion using exponentiation by squaring reduces the number of multiplications and is more efficient. Tail recursion is another optimized form that can be beneficial in certain situations.

By understanding these techniques, you can efficiently implement recursive solutions to calculate the power of a number, enhancing your ability to write clean and efficient code in C. Experiment with these approaches to deepen your understanding and improve your programming skills.