Answer:
The complete code along with output and comments for explanation are given below.
Step-by-step explanation:
#include <stdio.h>
// function DoublePennies starts here
// The function DoublePennies returns number of pennies if pennies are doubled numDays times
// this is an example of recursive function which basically calls itself
long long DoublePennies(long long numPennies, int numDays){
long long totalPennies = 0;
\\ here we implemented the base case when number of days are zero then return the number of pennies
if(numDays == 0)
return numPennies;
// if the base case is not executed then this else condition will be executed that doubles the number of pennies for each successive day.
else
{
totalPennies = DoublePennies((numPennies * 2), numDays - 1);
}
return totalPennies;
}
// driver code starts here
// Program computes pennies if you have 1 penny today,
// 2 pennies after one day, 4 after two days, and so on
int main(void)
{
// initialize starting pennies and number of days
long long startingPennies = 0;
int userDays = 0;
// input starting pennies and number of days
startingPennies = 1;
userDays = 10;
// print number of pennies and number of days
printf("Number of pennies after %d days: %lld\\", userDays, DoublePennies(startingPennies, userDays));
return 0;
}
Output:
Test 1:
Number of pennies after 10 days: 1024
Test 2:
Number of pennies after 2 days: 4
Test 3:
Number of pennies after 0 days: 1