codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
// The old 30 days program using a while loop. // You are offered a job working in the heat from sunup to sundown for 30 days. // Your salary starts at $0.01 per day and will double each day. // Would you take the job? Run this program and see if the job is worth it. #include <iostream> #include <iomanip> #include <string> using std::streamsize; using std::setprecision; int main() { double salary = .01; // daily salary double total = 0.0; // total of the current days salary and previous days const int width1 = 3; // used to format the day column const int width2 = 15; // used to format the salary and total columns streamsize prec = std::cout.precision(); // used to reset the output // print the column headers std::cout << std::endl; std::cout << std::setw(width1) << "Day" << std::setw(width2) << "Salary" << std::setw(width2) << "Total" << std::endl; // for each day, add the daily salary to the running total, print // the results, and then double the salary for the next day. int day = 1; while (day <= 30) { total += salary; // get the daily total // print the day, daily salary, and running total std::cout << std::setw(width1) << day << setprecision(12) << std::setw(width2) << salary << std::setw(width2) << total << setprecision(prec) << std::endl; salary *=2; // double the salary ++day; // increment the day; in this case it can be ++day or day++ } return 0; }
Private
[
?
]
Run code