Source of timing_summation1.cpp


  1: //timing_summation1.cpp
  2: //Finds the sum of integers over a given range.
  3: //Makes no attempt to measure the time taken to do this.

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

  8: int main()
  9: {
 10:     cout << "\nThis program computes the sum of all "
 11:             "integers in a user-chosen range.";
 12:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 18:     int sum = 0;
 19:     for (int numberToAdd=small; numberToAdd<=large; numberToAdd++)
 20:         sum += numberToAdd;

 22:     cout << "The sum of all integers from " << small << " to "
 23:          << large << " (inclusive) is " << sum << ".";
 24:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 25: }