1(x)
To execute a loop 10 times, which of the following is correct?
- (a)for (int i=11;i<=30;i+=2)
- (b)for (int i=11;i<=30;i+=3)
- (c)for (int i=11;i<20;i++)
- (d)for (int i=11;i<=21;i++)
Show SolutionHide Solution↓
for (int i=11;i<=30;i+=2)
Reason — To execute a loop 10 times, the total number of iterations should match 10, based on the condition and increment values. Let's analyse each option:
1. for (int i = 11; i <= 30; i += 2)
- The loop starts at
11and increments by2. - The sequence is:
11, 13, 15, 17, 19, 21, 23, 25, 27, 29. - This loop executes 10 times.
2. for (int i = 11; i <= 30; i += 3)
- The loop starts at
11and increments by3. - The sequence is:
11, 14, 17, 20, 23, 26, 29. - This loop executes only 7 times, so it is incorrect.
3. for (int i = 11; i < 20; i++)
- The loop starts at
11and increments by1. - The sequence is:
11, 12, 13, 14, 15, 16, 17, 18, 19. - This loop executes only 9 times, so it is incorrect.
4. for (int i = 11; i <= 21; i++)
- The loop starts at
11and increments by1. - The sequence is:
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21. - This loop executes 11 times, so it is incorrect.