Bifalgorithm post · Computer science one · Summation

How do you compute a summation with a for loop in C?

How do you compute a summation with a for loop in C? (Computer science one · Summation) — "Let’s revisit the example from Section 4.2.1 in which we computed th…

Ideas in this flowchart

  • Summation of integers with a for loop in C
  • The example computes 1 + 2 + · · · + 10
  • Code Sample 17.6 implements the summation
  • The variable i is declared as an integer
  • The variable sum is initialized to 0
  • The for loop starts at 1, continues while i ≤ 10, and increments i
  • Each iteration adds i to sum
  • The code can be generalized to sum up to a variable n
  • The generalized for loop uses n as its upper bound
Feed/

How do you compute a summation with a for loop in C?

No cover

Channel

Computer science one

1 page

"Let’s revisit the example from Section 4.2.1 in which we computed the sum of integers 1 + 2 + · · · + 10. The code is presented in Code Sample 17.6.\n\n1 int i;\n\n2 int sum = 0;\n\n3 for(i=1; i<=10; i++) {\n\n4 sum += i;\n\n5 }\n\nCode Sample 17.6.: Summation of Numbers using a For Loop in C\n\nOf course we could easily have generalized the code somewhat. Instead of computing a sum up to a particular number, we could have written it to sum up to another variable n, in which case the for loop would instead look like the following."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.