Power of a Number
Java coding interview problem for Number Logic: Power of a Number.
Calculating the Power of a Number is one of the most frequently asked Java coding interview questions. It helps interviewers evaluate your understanding of loops, arithmetic operations, recursion, optimization, and mathematical problem-solving.
Power calculations are widely used in mathematics, cryptography, scientific computing, graphics programming, machine learning, and competitive programming.
What is the Power of a Number?
The power of a number means multiplying the base by itself a specified number of times.
It is represented as
baseexponent
For example,
2^3
means
2 × 2 × 2
which equals
8
Mathematical Formula
Power = BaseExponent
Expanded form
BaseExponent
=
Base × Base × Base ...
(Exponent Times)
Examples
2^3 = 8
3^4 = 81
5^2 = 25
10^3 = 1000
7^1 = 7
Special Cases
Any Number Raised to 0
5^0 = 1
100^0 = 1
Any Number Raised to 1
9^1 = 9
Zero Raised to a Positive Number
0^5 = 0
One Raised to Any Number
1^100 = 1
Power Table
| Base | Exponent | Result |
|---|---|---|
| 2 | 1 | 2 |
| 2 | 2 | 4 |
| 2 | 3 | 8 |
| 2 | 4 | 16 |
| 2 | 5 | 32 |
| 3 | 2 | 9 |
| 3 | 3 | 27 |
| 4 | 3 | 64 |
| 5 | 3 | 125 |
Real Interview Question
Write a Java program to calculate the power of a number without using built-in methods.
Understanding the Logic
Suppose
Base = 3
Exponent = 4
The answer is
3 × 3 × 3 × 3
Instead of writing multiple multiplications manually, we use a loop.
Every iteration multiplies the current result by the base.
Visual Representation
Input
Base = 2
Exponent = 5
Processing
result = 1
↓
1 × 2 = 2
↓
2 × 2 = 4
↓
4 × 2 = 8
↓
8 × 2 = 16
↓
16 × 2 = 32
Output
32
Brute Force Approach
The simplest solution is
- Initialize the result as 1.
- Repeat the multiplication Exponent times.
- Multiply the result by the base during every iteration.
- Print the final result.
Algorithm
Step 1
Read the base.
Step 2
Read the exponent.
Step 3
Initialize
result = 1;
Step 4
Repeat
Exponent Times
Step 5
Multiply
result = result * base;
Step 6
Print the result.
Dry Run
Input
Base = 2
Exponent = 4
Initial
result = 1
| Iteration | Calculation | Result |
|---|---|---|
| 1 | 1 × 2 | 2 |
| 2 | 2 × 2 | 4 |
| 3 | 4 × 2 | 8 |
| 4 | 8 × 2 | 16 |
Output
16
Another Dry Run
Input
Base = 5
Exponent = 3
Initial
result = 1
| Iteration | Calculation | Result |
|---|---|---|
| 1 | 1 × 5 | 5 |
| 2 | 5 × 5 | 25 |
| 3 | 25 × 5 | 125 |
Output
125
Dry Run for Exponent = 0
Input
Base = 8
Exponent = 0
Loop
Runs 0 times
Result
1
Output
1
Approach 1 — Using Iteration (Loop)
This is the most common and interview-preferred solution because it is easy to understand and does not rely on any built-in methods.
Complete Java Program
public class PowerOfNumber {
public static void main(String[] args) {
int base = 3;
int exponent = 4;
long result = 1;
for (int i = 1; i <= exponent; i++) {
result = result * base;
}
System.out.println("Power = " + result);
}
}
Output
Power = 81
Step-by-Step Code Explanation
Step 1
Declare the base.
int base = 3;
Current value
3
Step 2
Declare the exponent.
int exponent = 4;
Current value
4
Step 3
Initialize the result.
long result = 1;
Initially
result = 1
Step 4
Start the loop.
for (int i = 1; i <= exponent; i++)
The loop executes exactly
4
times.
Step 5
Multiply the result.
result = result * base;
Iteration-wise calculation
1 × 3 = 3
↓
3 × 3 = 9
↓
9 × 3 = 27
↓
27 × 3 = 81
Step 6
Print the answer.
System.out.println("Power = " + result);
Output
Power = 81
Example Execution
Input
Base = 4
Exponent = 3
Processing
result = 1
↓
1 × 4 = 4
↓
4 × 4 = 16
↓
16 × 4 = 64
Output
64
Input
Base = 7
Exponent = 2
Processing
1 × 7 = 7
↓
7 × 7 = 49
Output
49
Why Does This Work?
The algorithm starts with the multiplicative identity,
1
Each iteration multiplies the current result by the base.
After repeating the multiplication exactly Exponent times, the final value becomes
BaseExponent
This approach directly follows the mathematical definition of exponentiation.
Advantages of This Approach
- Very easy to understand.
- Most common interview solution.
- No built-in methods required.
- Constant extra memory.
- Suitable for beginners.
- Works efficiently for small exponents.
Drawbacks
Although this solution is simple, it performs one multiplication for every exponent value. For very large exponents, this becomes inefficient.
Interviewers often ask follow-up questions such as:
- Can you solve it using recursion?
- Can you optimize it using Binary Exponentiation (Fast Power)?
- What is Exponentiation by Squaring?
- How does
Math.pow()work? - Which approach is best for large exponents?
- What is the time complexity of each approach?
In the next part, we'll cover:
- Recursive Solution
- Binary Exponentiation (Fast Power)
- Using
Math.pow() - Reusable Method
- Time and Space Complexity
- Comparison of All Approaches
- Common Interview Mistakes
- Frequently Asked Interview Questions
- Related Coding Problems
- Key Takeaways
- Interview Tips
Approach 2 — Using Recursion
Recursion is another popular interview solution.
Instead of using a loop, a recursive method calls itself until it reaches a base condition.
Mathematically,
base^exponent
=
base × base^(exponent − 1)
Base Condition
base^0 = 1
Recursive Formula
Example
2^4
Expansion
2 × 2^3
↓
2 × 2 × 2^2
↓
2 × 2 × 2 × 2^1
↓
2 × 2 × 2 × 2 × 2^0
↓
2 × 2 × 2 × 2 × 1
↓
16
Java Program
public class PowerRecursion {
static long 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 = 5;
System.out.println("Power = " + power(base, exponent));
}
}
Output
Power = 32
Dry Run for Recursion
Input
Base = 3
Exponent = 3
Method Calls
power(3,3)
↓
3 × power(3,2)
↓
3 × 3 × power(3,1)
↓
3 × 3 × 3 × power(3,0)
↓
3 × 3 × 3 × 1
↓
27
Approach 3 — Binary Exponentiation (Fast Power)
For very large exponents, repeatedly multiplying the base becomes inefficient.
An optimized algorithm called Binary Exponentiation (also known as Exponentiation by Squaring) reduces the number of multiplications significantly.
Instead of multiplying the base one exponent at a time, it repeatedly squares the base and halves the exponent.
Idea Behind Binary Exponentiation
If the exponent is even
base^8
=
(base^2)^4
If the exponent is odd
base^9
=
base × base^8
This reduces the number of operations from
O(n)
to
O(log n)
Dry Run
Input
Base = 2
Exponent = 10
Processing
Result = 1
Base = 2
Exponent = 10
↓
Square Base
Base = 4
Exponent = 5
↓
Multiply Result
Result = 4
↓
Square Base
Base = 16
Exponent = 2
↓
Square Base
Base = 256
Exponent = 1
↓
Multiply Result
Result = 1024
Output
1024
Java Program
public class FastPower {
public static void main(String[] args) {
int base = 2;
int exponent = 10;
long result = 1;
while (exponent > 0) {
if (exponent % 2 == 1) {
result *= base;
}
base *= base;
exponent /= 2;
}
System.out.println("Power = " + result);
}
}
Output
Power = 1024
Approach 4 — Using Math.pow()
Java provides a built-in method for exponentiation.
Java Program
public class MathPower {
public static void main(String[] args) {
int base = 5;
int exponent = 3;
double result = Math.pow(base, exponent);
System.out.println(result);
}
}
Output
125.0
Approach 5 — Using a Reusable Method
Reusable methods improve
- Readability
- Maintainability
- Unit Testing
- Code Reuse
Java Program
public class PowerMethod {
static long power(int base, int exponent) {
long result = 1;
for (int i = 1; i <= exponent; i++) {
result *= base;
}
return result;
}
public static void main(String[] args) {
System.out.println(power(4, 5));
}
}
Output
1024
Time Complexity
Iterative Approach
| Operation | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) |
Recursive Approach
| Operation | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) |
Additional space is used for the recursive call stack.
Binary Exponentiation
| Operation | Complexity |
|---|---|
| Time | O(log n) |
| Space | O(1) |
Math.pow()
| Operation | Complexity |
|---|---|
| Time | Optimized (Implementation Dependent) |
| Space | O(1) |
Comparison of All Approaches
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Iteration | O(n) | O(1) | Good for Beginners |
| Recursion | O(n) | O(n) | Elegant Solution |
| Binary Exponentiation | O(log n) | O(1) | ✅ Best for Interviews |
| Math.pow() | Optimized | O(1) | Production Use |
| Reusable Method | O(n) | O(1) | Reusable Code |
Common Mistakes
Mistake 1
Initializing the result as
long result = 0;
Wrong because
0 × anything = 0
Correct
long result = 1;
Mistake 2
Ignoring
Exponent = 0
Correct Answer
1
Always handle this special case.
Mistake 3
Writing the loop incorrectly.
Wrong
for(int i=0;i<base;i++)
Correct
for(int i=1;i<=exponent;i++)
The loop should execute Exponent times, not Base times.
Mistake 4
Using int for very large powers.
Example
20^10
may overflow an int.
Use
long
or
BigInteger
when appropriate.
Mistake 5
Using Math.pow() in coding interviews when the interviewer explicitly asks for a manual implementation.
Implement the algorithm using loops or binary exponentiation unless instructed otherwise.
Interview Follow-up Questions
Q1. Calculate power using recursion.
Q2. Implement Binary Exponentiation.
Q3. Explain why Binary Exponentiation is faster.
Q4. Calculate powers with negative exponents.
Q5. Implement your own version of Math.pow().
Q6. Find the square of a number without multiplication.
Q7. Find the cube of a number.
Q8. Compare recursion and iteration.
Q9. Handle very large powers using BigInteger.
Q10. Explain the time complexity of Binary Exponentiation.
Related Coding Problems
- Square Root of a Number
- Factorial
- Fibonacci Series
- Decimal to Binary
- Binary to Decimal
- Prime Number
- Armstrong Number
- Reverse Integer
Key Takeaways
- A power represents repeated multiplication of a base.
- Any number raised to 0 equals 1.
- The iterative approach is simple and easy to understand.
- The recursive approach directly follows the mathematical definition.
- Binary Exponentiation reduces the time complexity from O(n) to O(log n) and is the preferred optimized solution in interviews.
Math.pow()is convenient for production code but is usually not accepted when interviewers ask for a manual implementation.- Consider integer overflow for large powers and use
longorBigIntegerif needed.
Interview Tip
If an interviewer asks:
"Write a Java program to calculate the power of a number."
Start with the iterative approach because it is simple and demonstrates a clear understanding of loops. Once the basic solution is complete, explain the recursive solution and then introduce Binary Exponentiation (Exponentiation by Squaring) as the optimized approach. Highlight that Binary Exponentiation reduces the number of multiplications by squaring the base and halving the exponent, achieving O(log n) time complexity. Finally, mention Math.pow() as Java's built-in solution and discuss when it is appropriate to use it in production code versus coding interviews.