Read the question, write Python on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Write power(a, b) that returns a raised to the integer power b, using recursion. Anything to the power 0 is 1.
main already reads a and b and prints the result. Do not use ** or math.pow — recurse.
Input. Two lines: integers a and b, with b ≥ 0.
Output. One integer: a^b.
Constraints
- 0 ≤ b ≤ 12
- Results fit in a normal int.
Examples
Input
2 10
Output
1024
Input
5 0
Output
1
Hint
- Base case: if b == 0: return 1.
- Recursive case: return a * power(a, b - 1).
Show correct code
Peek only after you have tried. You can still Check your own version.
def power(a, b):
if b == 0:
return 1
return a * power(a, b - 1)
a = int(input())
b = int(input())
print(power(a, b))
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.