/**************************************************************************** * sigma2.c * * David J. Malan * malan@harvard.edu * * Adds the numbers 1 through n. * * Demonstrates recursion. ***************************************************************************/ #include #include // prototype int sigma(int); int main(void) { // ask user for a positive int int n; do { printf("Positive integer please: "); n = GetInt(); } while (n < 1); // compute sum of 1 through n int answer = sigma(n); // report answer printf("%d\n", answer); return 0; } /** * Returns sum of 1 through m; returns 0 if m is not positive. */ int sigma(int m) { // base case if (m <= 0) return 0; // recursive case else return (m + sigma(m-1)); }