Fibonacci Sequence Generator

Algorithm

Step 1: Start

Step 2: Declare integers a, b, c, n, i

Step 3: Initialize a = 0, b = 1

Step 4: Take input for range (n)

Step 5: Print a and b

Step 6: Repeat steps for i = 3 to n:

    Step 6.1: Compute c = a + b

    Step 6.2: Print c

    Step 6.3: Assign a = b, b = c

Step 7: End

Flowchart

graph TD; A([Start]) --> B[/Enter n/] B --> C[/User inputs n/] C --> D[Initialize a=0, b=1] D --> E[/Print 0, 1/] E --> M[i=3] M --> F{Is i ≤ n?} F ---->|YES| G[c = a + b] G --> H[/Print c/] H --> I[a = b] I --> J[b = c] J --> L[i++] L ----> F F -->|No| K([Stop]) %% Define styles classDef start fill:#00cc44,stroke:#006622,stroke-width:2px,color:#ffffff; classDef stop fill:#ff4444,stroke:#cc0000,stroke-width:2px,color:#ffffff; classDef input_output fill:#ffdd44,stroke:#cc9900,stroke-width:2px,color:#000000; classDef process fill:#4488ff,stroke:#0044cc,stroke-width:2px,color:#ffffff; classDef decision fill:#ff8844,stroke:#cc4400,stroke-width:2px,color:#ffffff; %% Apply styles class A start; class K stop; class B,C,E,H input_output; class D,G,I,J,L,M process; class F decision;

Program

#include<stdio.h>
void main()
{
    int a=0, b=1, c, n, i;
    printf("\nEnter Fibonacci Sequence range : ");
    scanf("%d", &n);
    
    // Display first two Fibonacci numbers
    printf("\n %d \t %d", a, b);
    
    // Loop to generate sequence
    for(i=3; i<=n; i++)
    {
        c = a + b;
        printf("\t %d", c);
        a = b;
        b = c;
    }
}
                

Test Case Output
Enter Fibonacci Sequence range: 7
0  1  1  2  3  5  8