Program2(a)
2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the console.
Objective of the Program
To understand looping constructs
To generate a sequence using iterative logic
To practice variable updating
To print series output in formatted form
Logical Construction (Think Before You Code)
Before writing the program, understand the pattern clearly.
The Fibonacci sequence is:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
How is it formed?
First number = 0
Second number = 1
Every next number = sum of previous two numbers, So the logic is:
Read N (how many numbers to print)
Initialize:
first = 0
second = 1
Print first two numbers
Use a loop to:
calculate next = first + second
print next
update first and second
Repeat until N numbers are printed
👉 The key idea: Each new number depends on the previous two numbers
Flowchart (Block Diagram of Logic)
Start
Read N
Initialize first = 0, second = 1
Print first
Print second
Repeat until count reaches N
Calculate next = first + second
Update variables
Stop
Algorithm
Start
Read value of N
Set first = 0
Set second = 1
Print first
Print second
For i from 3 to N
next = first + second
Print next
first = second
second = next
Stop
Python Program
⚠ Students must complete the missing parts.
# Program to generate Fibonacci sequence of length N
n = int(input("Enter the value of N: "))
first = 0
second = 1
print("Fibonacci Sequence:")
if n >= 1:
print(first, end=" ")
if n >= 2:
print(second, end=" ")
for i in range(3, ____________):
next = ____________ + ____________
print(next, end=" ")
first = ____________
second = ____________
Probable Output
Important Concepts Used
input()int()type conversionforloopVariable swapping
Sequence generation logic
Conditional statements
Viva Voce Questions
What is Fibonacci sequence?
Why do we initialize first as 0 and second as 1?
Why does loop start from 3?
What happens if N = 1?
Can this program generate negative Fibonacci numbers?
What is time complexity of this approach?
How can this be written using recursion?
Procedure to Execute the Program
Open Python IDLE / VS Code / Google Colab
Create a new Python file
Type the program
Save the file with
.pyextensionRun the program
Enter value of N
Google Colab – Online Execution
👉 Click below to execute the complete working version:
[https://colab.research.google.com/drive/1hdq_nD0sqO-y_i8i0PccHdItth8_TJ29?usp=sharing]
Program Explanation
Assignment for Students
Modify the program to print Fibonacci numbers less than 100
Write a recursive version of the same program
Modify to store the sequence in a list
Find the Nth Fibonacci number directly
Lab Record Instructions
Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:
Left Side of the Record
Write the following neatly on the left-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.

Comments
Post a Comment