Computer Science, asked by learner3668, 3 months ago

Predict the output of the following python program:

num,sum=10,0
for i in range(1,num+2,2):
if i % 3 == 0:
continue sum =
sum + i
print(sum)

Answers

Answered by poojan
14

Output:

1

6

13

24

Explanation:

num, sum = 10, 0

for i in range(1, num+2, 2):

   if i%3==0:

       continue

   sum=sum+1

   print(sum)

Here, num is initiated with 10 while sum with 0.

The looping range starts from 1 and ends with num+2 i.e., 10+2=12 [12 is excluded], with the gap of 2.

i.e., iteration goes on as [1, 3, 5, 7, 9, 11]

Now, the iterable value that is exactly divisible by 3 leaving remainder 0 will pa-ss the control to the next iteration directly, leaving the execution on the next statements due to the triggering of the continue statement.

1%3 is not equal to 0. So, sum = 0 + 1 => sum=1 and 1 is printed.

3%3==0. So, the loop directly goes for the next iteration, the sum remains 1.

5%3 is not equal to 0. So, sum = 1 + 5 => sum=6 and 6 is printed.

7%3 is not equal to 0. So, sum = 6 + 7 => sum=13 and 13 is printed.

9%3==0. So, the loop directly goes for the next iteration, the sum remains 13.

5%3 is not equal to 0. So, sum = 13 + 11 => sum=24 and 24 is printed.

Therefore the output is:

1

6

13

24

Learn more:

1) def find_max(nums):max_num float("-int") # smaller than all other numbersfor num in nums:if num > max_num:# (Fill in the missing line here)return...

https://brainly.in/question/35049689

2) The_________ is used to perform certain operations for a fixed number of times. 1) IF...ELSE2) WHILE LOOP3) NESTED IF...ELSE4) NONE OF THESE

https://brainly.in/question/36467283

Attachments:
Similar questions