0
0
CsharpProgramBeginner · 2 min read

C# Program to Find Power of Number

In C#, you can find the power of a number using Math.Pow(base, exponent) or by writing a loop to multiply the base number by itself exponent times.
📋

Examples

Inputbase=2, exponent=3
Output8
Inputbase=5, exponent=0
Output1
Inputbase=3, exponent=4
Output81
🧠

How to Think About It

To find the power of a number, think of multiplying the base number by itself as many times as the exponent says. For example, 2 to the power 3 means 2 × 2 × 2. You can do this by using a built-in function or by repeating multiplication in a loop.
📐

Algorithm

1
Get the base number and exponent from the user.
2
If using a loop, start with a result of 1.
3
Multiply the result by the base number repeatedly, exponent times.
4
Return or print the final result.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        double baseNum = 2;
        int exponent = 3;
        double result = Math.Pow(baseNum, exponent);
        Console.WriteLine($"{baseNum} to the power {exponent} is {result}");
    }
}
Output
2 to the power 3 is 8
🔍

Dry Run

Let's trace the example where base=2 and exponent=3 through the code using Math.Pow.

1

Set base and exponent

baseNum = 2, exponent = 3

2

Calculate power

result = Math.Pow(2, 3) which equals 8

3

Print result

Output: '2 to the power 3 is 8'

StepOperationValue
1Set baseNum and exponent2, 3
2Calculate Math.Pow(2,3)8
3Print output2 to the power 3 is 8
💡

Why This Works

Step 1: Using Math.Pow

The Math.Pow method takes two arguments: the base and the exponent, and returns the base raised to that exponent.

Step 2: Why it works

It internally multiplies the base by itself exponent times, handling decimal and negative exponents correctly.

Step 3: Output formatting

The result is printed with a message showing the base, exponent, and the calculated power.

🔄

Alternative Approaches

Loop multiplication
csharp
using System;
class Program {
    static void Main() {
        double baseNum = 2;
        int exponent = 3;
        double result = 1;
        for (int i = 0; i < exponent; i++) {
            result *= baseNum;
        }
        Console.WriteLine($"{baseNum} to the power {exponent} is {result}");
    }
}
This method uses a loop to multiply the base repeatedly; it is simple but only works for non-negative integer exponents.
Recursive method
csharp
using System;
class Program {
    static double Power(double baseNum, int exponent) {
        if (exponent == 0) return 1;
        return baseNum * Power(baseNum, exponent - 1);
    }
    static void Main() {
        double baseNum = 2;
        int exponent = 3;
        double result = Power(baseNum, exponent);
        Console.WriteLine($"{baseNum} to the power {exponent} is {result}");
    }
}
This uses recursion to calculate power; it is elegant but can cause stack overflow for very large exponents.

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

Time Complexity

Using a loop to multiply the base n times results in O(n) time, where n is the exponent. Math.Pow is optimized and faster internally.

Space Complexity

The space used is constant O(1) since only a few variables store the base, exponent, and result.

Which Approach is Fastest?

Math.Pow is the fastest and most reliable method as it uses optimized native code, while loops and recursion are slower and limited to integer exponents.

ApproachTimeSpaceBest For
Math.PowO(1) (optimized)O(1)All exponent types, including decimals and negatives
Loop multiplicationO(n)O(1)Simple integer exponents, beginner understanding
Recursive methodO(n)O(n) due to call stackLearning recursion, small integer exponents
💡
Use Math.Pow for simplicity and to handle all exponent types safely.
⚠️
Beginners often forget that exponent 0 always returns 1, regardless of the base.