/** * sigma-0.c * * David J. Malan * malan@harvard.edu * * Adds the numbers 1 through n. * * Demonstrates iteration. */ #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("%i\n", answer); } /** * Returns sum of 1 through m; returns 0 if m is not positive. */ int sigma(int m) { // return sum of 1 through m int sum = 0; for (int i = 1; i <= m; i++) { sum += i; } return sum; }