Problem 4 • Loops
Author: Er. Manoj Kumar • Computer Science Fundamentals
Generate the First N Terms of the Fibonacci Series
📋
Problem Statement
Given an integer N >= 1, print the first N numbers of the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13... using a three-variable sliding window.
📊
Variable Dictionary / Table
| Variable Name | Data Type | Role & Purpose |
|---|---|---|
| N | Integer | Total number of terms requested |
| T1, T2 | Long Long | First and second terms of window (0 and 1) |
| nextTerm | Long Long | Sum of previous two terms (T1 + T2) |
| count | Integer | Term counter running from 3 to N |
📝
Step-by-Step Formal Algorithm
Step 1: Start.
Step 2: Read positive integer N.
Step 3: If N <= 0:
Print 'Invalid count' and Stop.
Step 4: If N == 1:
Print '0' and Stop.
Step 5: Initialize T1 = 0, T2 = 1.
Step 6: Print T1, T2.
Step 7: Initialize count = 3.
Step 8: While (count <= N):
nextTerm = T1 + T2
Print nextTerm
T1 = T2
T2 = nextTerm
count = count + 1
Step 9: Stop.
🔀
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/"Read N"/]
In --> Ck1{"Is N <= 0 ?"}
Ck1 -- Yes --> Err[/"Print 'Invalid Input'"/]
Err --> Stop([Stop])
Ck1 -- No --> Ck2{"Is N == 1 ?"}
Ck2 -- Yes --> Out0[/"Print '0'"/]
Out0 --> Stop
Ck2 -- No --> Init["T1 = 0, T2 = 1
Print T1, T2
count = 3"] Init --> Loop{"Is count <= N ?"} Loop -- Yes --> Calc["nextTerm = T1 + T2
Print nextTerm
T1 = T2
T2 = nextTerm
count = count + 1"] Calc --> Loop Loop -- No --> Stop
Print T1, T2
count = 3"] Init --> Loop{"Is count <= N ?"} Loop -- Yes --> Calc["nextTerm = T1 + T2
Print nextTerm
T1 = T2
T2 = nextTerm
count = count + 1"] Calc --> Loop Loop -- No --> Stop
🖥️
Flowchart (ANSI Terminal ASCII Chart)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
+-----------------+
|
v
/ \
/ N <= 0? \ ---YES--> [Print Invalid] --> [STOP]
\ /
| NO
v
/ \
/ N == 1?\ ---YES--> [Print 0] --------> [STOP]
\ /
| NO
v
+-----------------+
| T1 = 0, T2 = 1 |
| Print T1, T2 |
| count = 3 |
+-----------------+
|
v
/ \
/count<=N?\ ---NO---> [STOP]
\ /
| YES
v
+-----------------+
| next = T1 + T2 |
| Print next |
| T1=T2, T2=next |
| count=count+1 |
+-----------------+
|
+------> (Loop to count <= N?)
🧪
Dry-Run Execution Trace Table
| Execution Step / Case | Variables State 1 | Variables State 2 | Condition Evaluation | Output / Result |
|---|---|---|---|---|
| N=7 | Init: T1=0, T2=1 | Printed: 0, 1 | count=3..7 | 0, 1, 1, 2, 3, 5, 8 |
⚡
Interactive C++ Compiler & Runner
Live In-BrowserEdit the code directly with the Edit Code button, provide custom inputs, and press Run Code to compile and execute in the live terminal console below!
📄
problem_4.cpp
GCC 13.2 • C++20
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of terms N: ";
if (!(cin >> n) || n <= 0) {
cout << "Please enter a positive integer (N >= 1).\n";
return 1;
}
long long t1 = 0, t2 = 1;
cout << "\nFibonacci Series (" << n << " terms):\n";
if (n == 1) {
cout << t1 << "\n";
return 0;
}
cout << t1 << ", " << t2;
for (int count = 3; count <= n; ++count) {
long long nextTerm = t1 + t2;
cout << ", " << nextTerm;
t1 = t2;
t2 = nextTerm;
}
cout << "\n";
return 0;
}
⌨️ Standard Input (cin):
Presets:
💻 Terminal Output
• Ready to run
Input: 8 Output: Fibonacci Series (8 terms): 0, 1, 1, 2, 3, 5, 8, 13
Developer & Exam Key Takeaway
Iterative sliding window yields O(N) time and O(1) space, vastly outperforming naive recursion which runs in exponential O(2^N) time.