Ask Question
5 September, 11:31

Write code to complete doublePennies () 's base case. Sample output for below program with inputs 1 and 10: Number of pennies after 10 days: 1024 Note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message.

+1
Answers (1)
  1. 5 September, 15:12
    0
    public class CalculatePennies {

    / / Returns number of pennies if pennies are doubled numDays times

    public static long doublePennies (long numPennies, int numDays) {

    long totalPennies = 0;

    / * Your solution goes here * /

    if (numDays = = 0) {

    totalPennies = numPennies;

    }

    else {

    totalPennies = doublePennies ((numPennies * 2), numDays - 1);

    }

    return totalPennies;

    }

    / / Program computes pennies if you have 1 penny today,

    / / 2 pennies after one day, 4 after two days, and so on

    public static void main (String [] args) {

    long startingPennies = 0;

    int userDays = 0;

    startingPennies = 1;

    userDays = 10;

    System. out. println ("Number of pennies after " + userDays + " days: "

    + doublePennies (startingPennies, userDays));

    return;

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write code to complete doublePennies () 's base case. Sample output for below program with inputs 1 and 10: Number of pennies after 10 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers