Quizzma Latest Questions

In the following code, what will be the last number to print to the screen before the program finishes?

Anonymous

In the following code, what will be the last number to print to the screen before the program finishes?

The code provided is:

python
for i in range(10):
if i % 2 == 0:
print(i)
else:
print(2 * i)



Related Questions

Leave an answer

Leave an answer

1 Answer

  1. Explanation:

    The loop iterates from i = 0 to i = 9 (since range(10) stops before 10).

    • If i is even (i % 2 == 0), it prints i.
    • If i is odd, it prints 2 * i.

    Let’s go through each iteration to see what prints:

    1. i=0i = 0i=0: Prints 0
    2. i=1i = 1i=1: Prints 2 * 1 = 2
    3. i=2i = 2i=2: Prints 2
    4. i=3i = 3i=3: Prints 2 * 3 = 6
    5. i=4i = 4i=4: Prints 4
    6. i=5i = 5i=5: Prints 2 * 5 = 10
    7. i=6i = 6i=6: Prints 6
    8. i=7i = 7i=7: Prints 2 * 7 = 14
    9. i=8i = 8i=8: Prints 8
    10. i=9i = 9i=9: Prints 2 * 9 = 18

    The last number printed is 18.

    Answer:

    18