English

Find the output of the following program segment: i = 0; sum = 0 while i < 9: if i % 4 == 0: sum = sum + i i = i + 2 print (sum)

Advertisements
Advertisements

Question

Find the output of the following program segment:

i = 0; sum = 0
while i < 9:
        if i % 4 == 0:
               sum = sum + i
        i = i + 2
print (sum)
Short/Brief Note
Advertisements

Solution

The ‘while’ loop will run till the value of 'i' is 8, and the condition in the ‘if’ function will return true for two values i.e. 4 and 8, which will be added to ‘sum’. The last statement will print the value of the variable ‘sum’.

OUTPUT:
12

shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Flow of Control - Exercise [Page 140]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 6 Flow of Control
Exercise | Q 5. (iv) | Page 140
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×