SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Repetition Structures
Exercise:
Programming Exercises
Question:7 | ISBN:9780132576376 | Edition: 2

Question

Pennies for Pay
Write a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Pennies for Pay program code:

#Prompt and read the number of days
numDays = int(input("Enter the number of days: "))
#Initialized variables
totalPay = 0
salary = 0.01
#Display a table showing what the salary was for 
#each day, and then show the total pay at the end
#of the period. The output should be displayed 
#in a dollar amount, not the number of pennies
print("Day\tSalary")
print("----------------")

for day in range(1, numDays + 1):
    totalPay += salary
    print(f"{day}\t${salary:.2f}")
    salary *= 2

totalPay = "${:,.2f}".format(totalPay)
print("\nTotal pay: ", totalPay)

Executed Output:

Enter the number of days: 20
Day	    Salary
----------------
1	    $0.01
2	    $0.02
3	    $0.04
4	    $0.08
5	    $0.16
6	    $0.32
7	    $0.64
8	    $1.28
9	    $2.56
10	    $5.12
11	    $10.24
12	    $20.48
13	    $40.96
14	    $81.92
15	    $163.84
16	    $327.68
17	    $655.36
18	    $1310.72
19	    $2621.44
20	    $5242.88

Total pay:  $10,485.75

 

0 0

Discussions

Post the discussion to improve the above solution.