Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

<p><span style=\"font-size: small; font-family: Times New Roman;\"><span style=\

ID: 3624472 • Letter: #

Question

<p><span><span>
<p>Write a program that ca lculates how much a person would earn over a period of time</p>
</span></span><span><span><span>if</span></span></span></p>
<p><span></span><span><span></span>
<p>&#160;</p>
</span>
<p>&#160;</p>
<p><em>
<p>&#160;</p>
</em><em><span>
<p>&#160;</p>
</span></em></p>
</p>
<p><span>his or her sa lary is one penny the first day and two pennies the second day, and continues </span></p>
<p>to double each day. The program should ask the user for the number of days.</p>
<p><span><span><span><font face="Times New Roman" size="3">
<p>Display</p>
</font></span></span><span><span>a </span></span><span><span>table showing how much the sa lary was for each da y, and then show the </span></span></span></p>
<p>total pay at the end of the period. The output should be displayed in a dollar amount,</p>
<p>not the number of pennies.</p>

Explanation / Answer

import java.util.Scanner;

public class Pennies{

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);

        int totalPenniesEarned=0, currentDaysEarnings=1, numDays;

        int currentDay;

        System.out.println("Please enter the number of days, > 0.");

        numDays = input.nextInt();

if (numDays <= 0) System.out.println("The number of days must be positive, try again later.");

        else {

            for (currentDay=1; currentDay <= numDays; currentDay++) {

               System.out.println("On day " + currentDay + " you earned " +

                                   currentDaysEarnings + ".");

                totalPenniesEarned += currentDaysEarnings;

                currentDaysEarnings = currentDaysEarnings * 2;

            }// end of loop

            System.out.println("You earned " + totalPenniesEarned +

" pennies, which is $"

                               + ((double)totalPenniesEarned/100.0));

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote