Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATH BASICS
When you generate Fibonacci numbers up to the inclusive limit 20, 20 is a value boundary. You print every Fibonacci value that is at most 20, regardless of how many terms that takes. Starting with 0 and 1, the required output is 0, 1, 1, 2, 3, 5, 8, 13. The next Fibonacci value is 21, which is larger than 20 and must not be printed.
0, 1, 1, 2, 3, 5, 8, 13
next value: 21, so stopThe sequence contains eight printed values here, but the number eight is only a result of the values in the sequence. It is not part of the instruction. A solution that prints exactly 20 terms would continue far beyond the value 20 and would solve a different problem.
Which output correctly represents the Fibonacci numbers up to the inclusive limit 20?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Each new Fibonacci term is the sum of the two terms before it. You therefore need to keep only those two terms, not the whole sequence. Start with first equal to 0 and second equal to 1. The next term is first + second. After using it, move second into first and move next into second.
int first = 0;
int second = 1;
int next = first + second;
first = second;
second = next;| FIRST BEFORE UPDATE | SECOND BEFORE UPDATE | GENERATED NEXT | FIRST AFTER UPDATE | SECOND AFTER UPDATE |
|---|---|---|---|---|
| 0 | 1 | 1 | 1 | 1 |
| 1 | 1 | 2 | 1 | 2 |
| 1 | 2 | 3 | 2 | 3 |
| 2 | 3 | 5 | 3 | 5 |
| 3 | 5 | 8 | 5 | 8 |
| 5 | 8 | 13 | 8 | 13 |
| 8 | 13 | 21 | 13 | 21 |
The table shows why the update order matters. The old second value must be copied into first before second is replaced by next. If you overwrite first or second too early, the addition no longer uses the two original stored terms, and the sequence changes.
The loop should ask whether the current term is within the boundary before it prints. With current starting at 0, the condition current <= 20 allows each valid term to be printed. The body then calculates the next term and shifts the stored state forward. Eventually current becomes 21, the condition is false, and the body is skipped.
int first = 0;
int second = 1;
int current = first;
while (current <= 20) {
cout << current << " ";
int next = first + second;
first = second;
second = next;
current = first;
}The final successful pass prints 13. Its update moves the state so that current becomes 21. The next condition check sees 21 <= 20 is false, so 21 never reaches the output statement. If you calculate and print first and apply the limit afterward, 21 has already escaped before the loop can reject it.
Replace the faulty loop condition so that 21 is not printed.
while (current <= 21) {
cout << current << " ";
// advance current
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
C++ and Java use the same state transition: start with 0 and 1, test the current value, print it, calculate the sum, then shift the two stored terms. Only the syntax for output differs. Both implementations print the same values and stop at the same failed condition, 21 <= 20.
int first = 0;
int second = 1;
int current = first;
while (current <= 20) {
cout << current << " ";
int next = first + second;
first = second;
second = next;
current = first;
}int first = 0;
int second = 1;
int current = first;
while (current <= 20) {
System.out.print(current + " ");
int next = first + second;
first = second;
second = next;
current = first;
}| ITERATION | CURRENT PRINTED | CURRENT AFTER UPDATE |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 2 |
| 4 | 2 | 3 |
| 5 | 3 | 5 |
| 6 | 5 | 8 |
| 7 | 8 | 13 |
| 8 | 13 | 21 |
| next check | not printed | 21 <= 20 is false |
Each iteration performs a fixed number of operations: one comparison, one output, one addition, and a few assignments. The loop runs once for each printed term, so the work grows with the number of terms up to the limit. The sequence itself does not need an array or list, because first and second are the only terms needed to generate the next value.