/* ========================================= Filename: sum_loop.c Filedate: January 18, 2007 The program asks for positive integer n and then computes the following sum: 2 + 3 + 6 + ... + (n*n - 2*n + 3) The computation uses a loop. ========================================= */ #include #include int main() { int i,n; int answer = 0; printf("\nInput a positive integer n.\n\n"); scanf("%d", &n); for (i = 1; i <= n; i++) { answer = answer + i*i - 2*i + 3; } printf("\nWhen n = %d,\n\n", n); printf("2 + 3 + 6 + ... + n*n - 2*n + 3 \n\n"); printf("is: %d\n", answer); return(0); }