Ask Question
17 October, 10:10

EX 6.1 How many iterations will the following for loops execute? for (int i = 0; i < 20; i + + ) { } for (int i = 1; i < = 20; i + + ) { } for (int i = 5; i 0; i - - ) { } for (int i = 1; i < 20; i = i + 2) { } for (int i = 1; i < 20; i * = 2) { }

+1
Answers (1)
  1. 17 October, 11:42
    0
    20, 20, 15, 20, 10, and 5 iterations respectively

    Step-by-step explanation:

    The loop "for (int i = 0; i < 20; i + + ) " starts at i=0 and increases the counter i by 1 at the end of each iteration. The counter must be less than 20, so the values of i in the loop are 0,1,2,3, ...,19. These are 20 values, and thus the code iterated 20 times.

    The loop "for (int i = 0; i < 20; i + + ) " starts at i=1 and increases the counter i by 1 at the end of each iteration. The counter can't exceed 20, so the values of i in the loop are 1,2,3,4, ...,20. These are 20 values, and thus the code iterated 20 times.

    Similarly, in "for (int i = 5; i < 20; i + + ) " i takes the 15 values are 5,6,7, ...,19. Hence, the code iterated 15 times.

    The loop "for (int i = 20; i > 0; i - - ) " starts at i=20 and decreases the counter i by 1 at the end of each iteration, so the values of i in the loop are 20,19,18, ...,1. These are 20 values, then we have 20 iterations.

    The loop "for (int i = 1; i < 20; i = i + 2) " starts at i=1 and increases the counter i by 2 at the end of each iteration. The values of i in the loop are 1,3,5,7, ...,19. These are 10 values, and thus the code iterated 10 times.

    The loop "for (int i = 0; i < 20; i + + ) " starts at i=1 and multiplies the counter i by 2 at the end of each iteration, so the values of i in the loop are 1,2,4,8,16. Then we have 5 iterations.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “EX 6.1 How many iterations will the following for loops execute? for (int i = 0; i < 20; i + + ) { } for (int i = 1; i < = 20; i + + ) { } ...” in 📘 Mathematics if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers