In: Computer Science
CHALLENGE ACTIVITY 9.4.1: Recursive function: Writing the base case. Add an if branch to complete double_pennies()'s base case. Sample output with inputs: 1 10 Number of pennies after 10 days: 1024.
the program language is python.
please highlight the code for me to copy. thanks
If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.
Solution :
Code:
# function double pennies that will double the number of pennies
until the number of days become 0
def double_pennies(initial_pennies,days):
# initialzie total number of pennies as 0
total = 0
# if the days become 0 then return initial_pennies
if days == 0:
return initial_pennies
# otherwise recursively call the function with doubled
initial_pennies and with a decrement of 1 in number of days
else:
total = double_pennies((initial_pennies * 2), (days - 1))
# return total number of pennies
return total
# input initial_pennies
initial_pennies=int(input())
# input days
days=int(input())
# print the message
print('Number of pennies after',days, 'days: ', end="")
# call the function
print(double_pennies(initial_pennies,days))
Output :