C#.NET — Identify the numeric series printed by the loop (recognize Fibonacci update pattern). int i = 1, j = 1, val; while (i < 25) { Console.Write(j + " "); val = i + j; j = i; i = val; }

Difficulty: Easy

Correct Answer: Fibonacci

Explanation:


Introduction / Context:
This classic loop prints a well-known series by repeatedly summing the previous two numbers. Your goal is to recognize the update pattern and map it to the correct series name without getting distracted by variable names.


Given Data / Assumptions:

  • Initial values: i = 1, j = 1.
  • Condition: while (i < 25) — loop runs while the next “i” remains under 25.
  • Prints j each iteration; then updates val = i + j; j = i; i = val.


Concept / Approach:
These assignments implement the Fibonacci recurrence where next = previous + current. The variable j holds the value that is printed, then the pair (i, j) is advanced to the next two Fibonacci numbers via the rotation j = i; i = val. The sequence begins 1, 1, 2, 3, 5, 8, 13, 21 … and stops before the next term would make i ≥ 25.


Step-by-Step Solution:

Start: (i, j) = (1, 1) → print 1. Compute val = 1 + 1 = 2 → set j = 1, i = 2. Next: print j = 1 → val = 2 + 1 = 3 → j = 2 → i = 3. Print 2 → val = 3 + 2 = 5 → j = 3 → i = 5. Print 3 → val = 5 + 3 = 8 → j = 5 → i = 8. Print 5 → val = 8 + 5 = 13 → j = 8 → i = 13. Print 8 → val = 13 + 8 = 21 → j = 13 → i = 21. Print 13 → val = 21 + 13 = 34 → j = 21 → i = 34 → stop (i not < 25).


Verification / Alternative check:
Compare to the canonical Fibonacci series start: 1, 1, 2, 3, 5, 8, 13, 21 — matches exactly.


Why Other Options Are Wrong:
Prime/palindrome/odd/even classification does not fit this two-term additive recurrence.


Common Pitfalls:
Confusing which variable prints vs which advances; reading “i < 25” as a print limit instead of the control for advancing.


Final Answer:
Fibonacci

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion