Source of Question01.cpp


  1: //  Created by Frank M. Carrano and Tim Henry.
  2: //  Copyright (c) 2013 __Pearson Education__. All rights reserved.

  4: /** Computes the sum of the integers from 1 through n.
  5:  @pre  n > 0.
  6:  @post  None.
  7:  @param n  A positive integer.
  8:  @return  The sum 1 + 2 + . . . + n. */
  9: int sumUpTo(int n)
 10: {
 11:    int sum = 0;
 12:    if (n == 1)
 13:       sum = 1;
 14:    else // n > 1
 15:       sum = n + sumUpTo(n - 1);
 16:    return sum;
 17: }  // end sumUpTo