/* testrep4.cpp CIS 150 6/7/2005 David Klick Demonstration of accumulator (for sum). */ #include using std::cout; int main(void) { int ctr; // loop counter int sum; // accumulator // add up all the numbers from 1 to 10 using for sum = 0; for (ctr=1; ctr<=10; ctr++) { sum += ctr; } cout << "The sum of 1 through 10 is " << sum << '\n'; // add up all the numbers from 1 to 10 using while sum = 0; ctr = 1; while (ctr<=10) { sum += ctr; ctr++; } cout << "The sum of 1 through 10 is " << sum << '\n'; // add up all the numbers from 1 to 10 using do/while sum = 0; ctr = 1; do { sum += ctr; ctr++; } while (ctr <= 10); cout << "The sum of 1 through 10 is " << sum << '\n'; return 0; } /* Sample output: The sum of 1 through 10 is 55 The sum of 1 through 10 is 55 The sum of 1 through 10 is 55 */