C++ Program to Find Power of Number
You can find the power of a number in C++ by using a loop to multiply the base number by itself repeatedly, like
for (int i = 1; i <= exponent; i++) result *= base;.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. If the exponent is zero, the answer is always 1. Otherwise, start with 1 and multiply by the base repeatedly until you reach the exponent count.
Algorithm
1
Get the base number and exponent from the user.2
If the exponent is zero, return 1 immediately.3
Initialize a result variable to 1.4
Multiply the result by the base number repeatedly, exponent times.5
Return or print the result.Code
cpp
#include <iostream> using namespace std; int main() { int base, exponent; cout << "Enter base and exponent: "; cin >> base >> exponent; int result = 1; for (int i = 1; i <= exponent; i++) { result *= base; } cout << base << "^" << exponent << " = " << result << endl; return 0; }
Output
Enter base and exponent: 2 3
2^3 = 8
Dry Run
Let's trace the input base=2 and exponent=3 through the code
1
Input
base = 2, exponent = 3
2
Initialize result
result = 1
3
First loop iteration
result = 1 * 2 = 2
4
Second loop iteration
result = 2 * 2 = 4
5
Third loop iteration
result = 4 * 2 = 8
6
Output result
Print 2^3 = 8
| Iteration | Result |
|---|---|
| 0 | 1 |
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
Why This Works
Step 1: Initialize result to 1
We start with result = 1 because multiplying by 1 does not change the value and it is the identity for multiplication.
Step 2: Multiply in loop
Each loop multiplies result by base, repeating this exponent times to get the power.
Step 3: Handle zero exponent
If the exponent is zero, the loop does not run and result stays 1, which is correct since any number to the power 0 is 1.
Alternative Approaches
Using std::pow function
cpp
#include <iostream> #include <cmath> using namespace std; int main() { double base, result; int exponent; cout << "Enter base and exponent: "; cin >> base >> exponent; result = pow(base, exponent); cout << base << "^" << exponent << " = " << result << endl; return 0; }
This uses the built-in <code>pow</code> function from <code>cmath</code>, which is simpler but works with floating-point numbers and may introduce rounding.
Using recursion
cpp
#include <iostream> using namespace std; int power(int base, int exponent) { if (exponent == 0) return 1; return base * power(base, exponent - 1); } int main() { int base, exponent; cout << "Enter base and exponent: "; cin >> base >> exponent; cout << base << "^" << exponent << " = " << power(base, exponent) << endl; return 0; }
This uses recursion to calculate power, which is elegant but can be slower and risk stack overflow for large exponents.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs exactly exponent times, so the time grows linearly with the exponent.
Space Complexity
Only a few variables are used, so space is constant O(1).
Which Approach is Fastest?
Using the built-in pow function is usually fastest and optimized, but the loop method is simple and clear for integers.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Loop multiplication | O(n) | O(1) | Simple integer powers |
| std::pow function | O(1) or optimized | O(1) | Floating-point and general use |
| Recursion | O(n) | O(n) | Elegant code but less efficient |
Remember that any number raised to the power 0 is always 1.
Beginners often forget to handle the case when the exponent is zero, which should return 1.