Create one file containing a main function and a recursive function named power
ID: 3668516 • Letter: C
Question
Create one file containing a main function and a recursive function named power.
In main:
Ask the user to enter a number for the base of the power.
Ask the user for a number for the positive exponent of the power.
Call the power function and print its returned value.
The recursive power function, power(base, exponent), must recursively calculate the value of the power and then return it.
SAMPLE OUTPUT
Enter an integer for the base: 6
Enter an integer for the exponent: 3
6 to the power 3 equals 216
Explanation / Answer
This below python code will work recusively calcualte the power of given value with respect to base value and return result to main method..
see the below python code for power(base, exponent):
def power(x, y):
if y == 0:
return 1
else:
return x * power(x, y-1)
if __name__ == "__main__":
base = raw_input("Enter an integer for the base: ")
exponent = raw_input("Enter an integer for the exponent: ")
result = power(base, exponent)
print base+" to the power "+exponent+" equals "+result
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.