Source of sum_integers.cpp


  1: //sum_integers.cpp
  2: //Computes the sum of all integers between and including two integers 
  3: //input by the user. Illustrates a count-controlled do...while-loop.

  5: #include <iostream>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program computes the sum of all integers in a "
 11:         "range of\nintegers entered by the user.\n\n";

 13:     int small, large;
 14:     cout << "Enter the smaller integer, then the larger: ";
 15:     cin >> small >> large;  cin.ignore(80, '\n');  cout << endl;

 17:     int numberToAdd = small;
 18:     int max = large;
 19:     int sum = 0;

 21:     do
 22:     {
 23:         sum = sum + numberToAdd;
 24:         numberToAdd++;
 25:     } while (numberToAdd <= max);

 27:     cout << "The sum of all integers from " << small << " to "
 28:          << large << " (inclusive) is " << sum << ".\n";
 29:     cout << endl;
 30: }