Write a program that calculates how much a person earns in a month if the salary
ID: 3552962 • Letter: W
Question
Write a program that calculates how much a person earns in a month if the salary is one penny the first day, two pennies the second day, four pennies the third day, and so on with the daily pay doubling each day the employee works. The program should ask the user for the number of days the employee worked during the month and should display a table showing how much the salary was for each day worked, as well as the total pay earned for the month. The output should be displayed in dollars with two decimal points, not in pennies.
Input Validation: Do not accept a number less than 1 or more than 31 for the number of days worked.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int days;
cout<<"Enter The number of days you have worked this month:"<<endl;
cin>>days;
while(days < 1 || days > 31)
{ cout<<"Only enter a number between 1 and 31"<<endl;
cin>>days;}
int num = 1,sum=0;
for(int i = 1; i <= days; i++){
cout<<"Day "<<i<<" earned "<<num<<" pennies"<<endl;
sum+=num;
num = num * 2;
}
cout << "Total pennies earned in "<<days<<" days:"<<sum<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.