153k views
1 vote
C = 1 sum = 0 while (c < 10): c = c + 2 sum = sum + c print (sum)

User Bob Brinks
by
7.3k points

2 Answers

2 votes


\huge \boxed{\sf 35}

c = 1, sum = 0

(The loop runs)

1 < 10, c = 1 + 2, sum = 0 + 3

c = 3, sum = 3

3 > 10, c = 3 + 2, sum = 3 + 5

c = 5, sum = 8

5 > 10, c = 5 + 2, sum = 8 + 7

c = 7, sum = 15

7 > 10, c = 7 + 2, sum = 15 + 9

c = 9, sum = 24

9 > 10, c = 9 + 2, sum = 24 + 11

c = 11, sum = 35

(The condition is false and the loop ends)

11 > 10, print sum

User HyperioN
by
7.6k points
3 votes

Answer:

35

Step-by-step explanation:

The loop runs 5 times with the following values of c: 1,3,5,7,9 at the start of the iteration, and 2 higher at the end. So the values that get added to sum are: 3,5,7,9,11, hence sum = 3+5+7+9+11 = 35.

Always look carefully at the last iteration of a loop. At the last iteration, when c equals 9, it is still valid to make another iteration. However, 11 gets added to the sum.

User Mafor
by
8.4k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.