<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> </p>
</span>
<p> </p>
<p><em>
<p> </p>
</em><em><span>
<p> </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));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.