Power x^n Recursively
Calculate x raised to power n recursively.
IntermediatePhase 3 - Level 1: Basic RecursionExample 7 of 10
power-x-n-recursively.py
1def power(x, n):2 # Base case3 if n == 0:4 return 15 if n == 1:6 return x78 # Recursive case9 return x * power(x, n - 1)1011# Test12x = int(input("Enter base: "))13n = int(input("Enter exponent: "))14result = power(x, n)15print(f"{x}^{n} = {result}")
Output
Enter base: 2 Enter exponent: 5 2^5 = 32
What's going on
Power(x, n) = x * Power(x, n-1).
Key Concepts:
Base case: x^0 = 1, x^1 = x
Recursive: x * x^(n-1)
Multiply x with result of smaller power
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum