/**************************************************************************** * sigma2.c * * Computer Science 50 * David J. Malan * * Adds the numbers 1 through n. * * Demonstrates recursion. ***************************************************************************/ #include #include /* prototype */ int sigma(int); int main(int argc, char * argv[]) { int answer, n; /* ask user for a positive int */ do { printf("Positive integer please: "); n = GetInt(); } while (n < 1); /* compute sum of 1 through n */ answer = sigma(n); /* report answer */ printf("%d\n", answer); } /* * int * sigma(int m) * * 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)); }