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:

  1. Read N (how many numbers to print)

  2. Initialize:

    • first = 0

    • second = 1

  3. Print first two numbers

  4. Use a loop to:

    • calculate next = first + second

    • print next

    • update first and second

  5. Repeat until N numbers are printed

👉 The key idea: Each new number depends on the previous two numbers


Flowchart (Block Diagram of Logic)


Flowchart Explanation
  • 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

  1. Start

  2. Read value of N

  3. Set first = 0

  4. Set second = 1

  5. Print first

  6. Print second

  7. For i from 3 to N

    • next = first + second

    • Print next

    • first = second

    • second = next

  8. 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

Case 1
Enter the value of N: 5
Fibonacci Sequence:
0 1 1 2 3

Case 2
Enter the value of N: 8
Fibonacci Sequence:
0 1 1 2 3 5 8 13

Case 3 (Edge Case)
Enter the value of N: 1
Fibonacci Sequence:
0


Important Concepts Used

  • input()

  • int() type conversion

  • for loop

  • Variable swapping

  • Sequence generation logic

  • Conditional statements


Viva Voce Questions

  1. What is Fibonacci sequence?

  2. Why do we initialize first as 0 and second as 1?

  3. Why does loop start from 3?

  4. What happens if N = 1?

  5. Can this program generate negative Fibonacci numbers?

  6. What is time complexity of this approach?

  7. How can this be written using recursion?


Procedure to Execute the Program

  1. Open Python IDLE / VS Code / Google Colab

  2. Create a new Python file

  3. Type the program

  4. Save the file with .py extension

  5. Run the program

  6. 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.


-:END:-

Comments

Popular posts from this blog