ITPEC FE Subject A April 2024 Question 6

Source exam: ITPEC FE Subject A April 2024Topic: Basic Theory & Math

ITPEC FE Subject A April 2024 — Question 6 of 60

Flowchart: Sum of First N Odd Numbers — find the loop exit condition for summing 1 + 3 + 5 + ... + (2N-1).

The algorithm uses counter i starting at 1, incrementing by 2 each iteration, accumulating into x.

  • Last value to add: 2N - 1
  • After adding it: i becomes 2N - 1 + 2 = 2N + 1
  • Exit when: i ≥ 2N + 1

Trace for N = 3 (expect 1 + 3 + 5 = 9):
- i=1x=1, i=3

- i=3x=4, i=5

- i=5x=9, i=7

- i=7 ≥ 7 → exit with x = 9

Why not others:
- (a) i ≥ 2N − 1 — exits before adding the last odd number

- (c) x ≥ 2N − 1 — compares the sum, not the counter

- (d) x ≥ 2N + 1 — compares the sum, not the counter

Key rule: In counter-based loops, the exit condition depends on the counter variable, not the accumulated result.

AI-generated — may contain errors

The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.