Java Program to Find Power of Number
You can find the power of a number in Java by using a loop to multiply the base number by itself exponent times, for example:
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, multiply the base number by itself repeatedly as many times as the exponent indicates. If the exponent is zero, the result is always 1. This approach uses simple repeated multiplication.
Algorithm
1
Get the base number and exponent as input.2
Initialize a result variable to 1.3
Repeat multiplying the result by the base number exponent times.4
Return or print the result.Code
java
public class PowerCalculator { public static void main(String[] args) { int base = 2; int exponent = 3; int result = 1; for (int i = 1; i <= exponent; i++) { result *= base; } System.out.println("Result: " + result); } }
Output
Result: 8
Dry Run
Let's trace base=2 and exponent=3 through the code
1
Initialize variables
base=2, exponent=3, result=1
2
First loop iteration
result = 1 * 2 = 2
3
Second loop iteration
result = 2 * 2 = 4
4
Third loop iteration
result = 4 * 2 = 8
5
Loop ends
Final result = 8
| Iteration | Result |
|---|---|
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
Why This Works
Step 1: Start with result as 1
We set result = 1 because multiplying by 1 does not change the value and it is the neutral element for multiplication.
Step 2: Multiply result by base repeatedly
Each loop multiplies result by base, simulating the power operation by repeated multiplication.
Step 3: Return the final result
After the loop finishes, result holds the base raised to the exponent, which we print.
Alternative Approaches
Using Math.pow() method
java
public class PowerCalculator { public static void main(String[] args) { int base = 2; int exponent = 3; double result = Math.pow(base, exponent); System.out.println("Result: " + (int)result); } }
This uses Java's built-in method for power, which is simpler and handles decimals but returns a double.
Using recursion
java
public class PowerCalculator { public static int power(int base, int exponent) { if (exponent == 0) return 1; return base * power(base, exponent - 1); } public static void main(String[] args) { int base = 2; int exponent = 3; System.out.println("Result: " + power(base, exponent)); } }
This uses a recursive function to calculate power, which is elegant but can cause stack overflow for large exponents.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs exponent times, so the time complexity is O(n) where n is the exponent.
Space Complexity
Only a few variables are used, so space complexity is O(1).
Which Approach is Fastest?
Using Math.pow() is fastest and optimized internally, while loops and recursion are straightforward but slower for large exponents.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Loop multiplication | O(n) | O(1) | Simple integer powers |
| Math.pow() method | O(1) | O(1) | All numeric powers, including decimals |
| Recursion | O(n) | O(n) | Elegant code, small exponents |
Remember that any number raised to the power 0 is always 1.
Beginners often forget to initialize the result to 1, causing incorrect multiplication results.