C Program
#include <stdio.h> long power(int base, int exp) { if(exp == 0) return 1; return base * power(base, exp - 1); } int main() { int base = 2, exp = 5; printf("%d^%d = %ld\n", base, exp, power(base, exp)); return 0; }
C Output
Input: Base = 2 Exponent = 5 Output: 2^5 = 32
C++ Program
#include <iostream> using namespace std; long power(int base, int exp) { if(exp == 0) return 1; return base * power(base, exp - 1); } int main() { int base = 3, exp = 4; cout << base << "^" << exp << " = " << power(base, exp); return 0; }
C++ Output
Input: Base = 3 Exponent = 4 Output: 3^4 = 81
JAVA Program
public class Main { static long power(int base, int exp) { if(exp == 0) return 1; return base * power(base, exp - 1); } public static void main(String[] args) { int base = 5, exp = 3; System.out.println(base + "^" + exp + " = " + power(base, exp)); } }
JAVA Output
Input: Base = 5 Exponent = 3 Output: 5^3 = 125
Python Program
def power(base, exp): if exp == 0: return 1 return base * power(base, exp - 1) base, exp = 4, 2 print(f"{base}^{exp} = {power(base, exp)}")
Python Output
Input: Base = 4 Exponent = 2 Output: 4^2 = 16
Explanation
Example
The strength of a number is to multiply the base by itself some number of times, as specified by the exponent. For instance, 2^5 is 2 × 2 × 2 × 2 × 2 = 32. Recursively, we apply the rule that base^exp = base × base^(exp-1) until exp equals 0, at which point we return 1 by definition. The recursion unspools, multiplying all the returned values together.
Real-Life Analogy
Consider power calculation as ascending stairs in which every step signifies multiplying by the base once. If you must get to the 5th step, you take one step and instruct the next person to ascend the remaining 4 steps for you. That individual takes one step and leaves the rest to another, and so on. When a person finally arrives at the 0th step (no steps remaining), they pay back 1, and everyone on the way back pays back by the base they brought.
Why It Matters
This program is a brilliant introduction to recursion for mathematical problems that involve repeated multiplication. It illustrates how apparently large calculation can be rephrased as a smaller, similar problem. Once you get used to this style, it will be easy to tackle other problems such as factorial, Fibonacci, and even more complex algorithms like fast exponentiation and polynomial evaluation.
Complexity and Optimization.
The time complexity of this basic recursive power function is O(exp) since it has one call per decrement of exponent. Space complexity is also O(exp) because of the call stack. The method can be slow for extremely large exponents, but the optimized solution like Exponentiation by Squaring reduces the time complexity to O(log exp). Yet, the basic recursive version is more newbie-friendly and demonstrates the basic recursion paradigm.
Edge Cases and Testing
The scenario when the exponent is 0 must always yield 1 according to mathematical convention. A negative exponent would involve dealing with fractions (such as 2^-3 = 1/8), which can be added as an extension. Testing should cover small bases, big exponents, and the edge case of zero exponent to ensure correctness.
Interview Tips
In interviews, once you have written down this simple recursive solution, anticipate follow-up questions such as "Can you do this faster?" or "How would you do negative exponents?" or even "Can you do this without recursion?" Having both the naive recursive and optimal approach ready demonstrates excellent problem-solving skills.
SEO-Friendly Closing
Recursion power of a number is a basic programming problem in C, C++, Java, and Python that enhances understanding of base cases and recursive calls. Practicing this program familiarizes students with decomposing repetitive multiplication into smaller subproblems, an imperative concept in algorithms and computational mathematics. This problem usually comes in coding interviews, algorithm tutorials, and competitive programming problems, so it's a crucial step for learning recursive problem-solving skills.
Social Plugin