fork download
  1. //Devin Scheu CS1A Chapter 5, P. 295, #7
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE DOUBLED PENNY SALARY OVER TIME
  6. * ____________________________________________________________
  7. * This program determines the daily salary and total earnings
  8. * when the salary starts at one penny and doubles each day.
  9. *
  10. * Computation is based on a loop doubling the salary each day
  11. * and converting the total to dollars
  12. * ____________________________________________________________
  13. * INPUT
  14. * daysWorked : The number of days worked
  15. *
  16. * OUTPUT
  17. * dailySalaryTable : The table of daily salaries in dollars
  18. * totalEarnings : The total earnings in dollars
  19. *
  20. **************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <cmath>
  25.  
  26. using namespace std;
  27.  
  28. int main () {
  29.  
  30. //Variable Declarations
  31. int daysWorked; //INPUT - The number of days worked
  32. double dailySalary; //OUTPUT - The salary for each day in dollars
  33. double totalEarnings; //OUTPUT - The total earnings in dollars
  34.  
  35. //Prompt for Input
  36. cout << "Enter the number of days worked: ";
  37. cin >> daysWorked;
  38.  
  39. //Input Validation
  40. while (daysWorked < 1) {
  41. cout << "\nError: Please enter a number greater than or equal to 1: ";
  42. cin >> daysWorked;
  43. }
  44.  
  45. //Initialize Variables
  46. totalEarnings = 0.0;
  47.  
  48. //Display Header
  49. cout << fixed << setprecision(2);
  50. cout << "\nDay\tDaily Salary ($)" << endl;
  51. cout << "------------------------" << endl;
  52.  
  53. //Calculate and Display Daily Salary
  54. for (int day = 1; day <= daysWorked; day++) {
  55. dailySalary = (0.01 * pow(2, day - 1)); // Convert pennies to dollars and double each day
  56. totalEarnings += dailySalary;
  57. cout << left << setw(4) << day << right << setw(16) << dailySalary << endl;
  58. }
  59.  
  60. //Display Total Earnings
  61. cout << "------------------------" << endl;
  62. cout << left << setw(4) << "Total" << right << setw(16) << totalEarnings << endl;
  63.  
  64. return 0;
  65. } //end of main()
Success #stdin #stdout 0.01s 5316KB
stdin
17
stdout
Enter the number of days worked: 
Day	Daily 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
------------------------
Total         1310.71