0
0
CsharpProgramBeginner · 2 min read

C# Program to Calculate Power Using Recursion

You can calculate power using recursion in C# by defining a method like Power(int baseNum, int exponent) that calls itself with exponent - 1 until it reaches zero, returning 1 as the base case.
📋

Examples

InputbaseNum = 2, exponent = 3
Output8
InputbaseNum = 5, exponent = 0
Output1
InputbaseNum = 3, exponent = 1
Output3
🧠

How to Think About It

To find the power of a number using recursion, think of multiplying the base number by itself exponent times. If the exponent is zero, return 1 because any number to the power of zero is 1. Otherwise, multiply the base by the result of the function called with exponent minus one.
📐

Algorithm

1
Check if exponent is zero; if yes, return 1.
2
Otherwise, call the power function recursively with exponent minus one.
3
Multiply the base number by the result of the recursive call.
4
Return the multiplication result.
💻

Code

csharp
using System;
class Program {
    static int Power(int baseNum, int exponent) {
        if (exponent == 0) return 1;
        return baseNum * Power(baseNum, exponent - 1);
    }
    static void Main() {
        Console.WriteLine(Power(2, 3));
    }
}
Output
8
🔍

Dry Run

Let's trace Power(2, 3) through the code

1

Initial call

Power(2, 3) checks if exponent == 0 (false), so returns 2 * Power(2, 2)

2

Second call

Power(2, 2) checks if exponent == 0 (false), so returns 2 * Power(2, 1)

3

Third call

Power(2, 1) checks if exponent == 0 (false), so returns 2 * Power(2, 0)

4

Base case

Power(2, 0) returns 1 because exponent == 0

5

Return values

Power(2, 1) returns 2 * 1 = 2 Power(2, 2) returns 2 * 2 = 4 Power(2, 3) returns 2 * 4 = 8

CallExponentReturn Value
Power(2, 0)01
Power(2, 1)12
Power(2, 2)24
Power(2, 3)38
💡

Why This Works

Step 1: Base Case

The recursion stops when exponent == 0, returning 1 because any number to the power zero is 1.

Step 2: Recursive Call

For other cases, the function calls itself with exponent - 1, reducing the problem size.

Step 3: Multiplication

Each recursive call multiplies the base number by the result of the smaller problem, building up the final power value.

🔄

Alternative Approaches

Using a loop
csharp
using System;
class Program {
    static int PowerLoop(int baseNum, int exponent) {
        int result = 1;
        for (int i = 0; i < exponent; i++) {
            result *= baseNum;
        }
        return result;
    }
    static void Main() {
        Console.WriteLine(PowerLoop(2, 3));
    }
}
This uses iteration instead of recursion, which can be more efficient for large exponents because it avoids function call overhead.
Using Math.Pow
csharp
using System;
class Program {
    static void Main() {
        double result = Math.Pow(2, 3);
        Console.WriteLine((int)result);
    }
}
This uses the built-in Math.Pow method, which is optimized but returns a double, so casting to int may be needed.

Complexity: O(n) time, O(n) space

Time Complexity

The function calls itself exponent times, so the time grows linearly with the exponent.

Space Complexity

Each recursive call adds a new layer to the call stack, so space used is proportional to the exponent.

Which Approach is Fastest?

The iterative loop approach is faster and uses less memory than recursion because it avoids call stack overhead.

ApproachTimeSpaceBest For
RecursionO(n)O(n)Simple code, small exponents
LoopO(n)O(1)Better performance, large exponents
Math.PowO(1)O(1)Built-in, optimized, floating-point
💡
Always include a base case in recursion to avoid infinite calls.
⚠️
Forgetting the base case causes the recursion to never stop and crash the program.