/* summation.cpp is a driver program to test function sum().
 *
 *   Input:   an integer n
 *   Output:  the sum of the integers from 1 through n
 ****************************************************************/

#include <iostream>          // cout, cin, <<, >>
#include <cassert>           // assert
using namespace std;

int sum(int n);               // prototype for Sum()

int main()
{
  cout << "This program computes the sum of the integers from "
          "1 through n.\n";

  cout << "Enter a value for n: ";
  int n;
  cin >> n;

  cout << "--> 1 + ... + " << n << " = " << sum(n) << endl;



  return 0;
}

/* sum(n) computes the sum of the integers from 1 to n.
 * 
 * Receive:      n, an integer
 * Precondition: n > 0 
 * Return:       the sum 1 + 2 + ... + n
 *********************************************************/

int sum (int n)
{
   assert(n > 0);

   int runningTotal = 0;

   for (int count = 1; count <= n; count++)
      runningTotal += count;

   return runningTotal;
}
