C Program to Display Fibonacci Series up to N

Fibonacci sequence

C program to display fibonacci sequence up to n has been shown here. Here n is the limit up to which the sequence is to be generated. For example if n = $20$, we get the fibonacci numbers up to $20$ i.e. $0, 1, 1, 2, 3, 5, 8, 13$. The following section covers the iterative approach to find fibonacci sequence. The algorithm, pseudocode of the program have been shown below.






1. Algorithm to display fibonacci sequence upto n


1. Take the limit n as input.

2. Assign the first two fibonacci numbers to variables a, b i.e. a = 0 and b = 1

3. If n = 0, display a else, display a, b.

4. Check if a + b <= n

5. If step 4 is true perform step 6 to 8, else stop the process

6. t = a + b and display t

7. a = b and b = t

8. Go to step 4




2. Pseudocode to display fibonacci sequence upto n


Input : A limit $n$

Output : Fibonacci sequence upto $n$

1. Procedure fibonacciUptoN($n$):

2. $a \leftarrow 0$

3. $b \leftarrow 1$

4. If $n == 0$:

5. Display $a$

6. Else:

7.Display $a, b$

8. Repeat until $a + b \leq n$:

9. $t \leftarrow a + b$

10.Display $t$

11.$a \leftarrow b$

12.$b \leftarrow t$

13. End Procedure





3. C Program & output to display fibonacci sequence upto n using iteration

Code has been copied
/*****************************
    alphabetacoder.com
C program to find fibonacci 
series up to N using iteration
******************************/

#include <stdio.h>

int main() {
    // declare variables
    int n, a, b, t = 0;

    // take input of the limit
    printf("Enter the limit = ");
    scanf("%d", & n);

    // intialize the first two terms of the sequence
    a = 0;
    b = 1;

    printf("Fibonacci sequence upto %d: ", n);
    // display the first fibonacci
    if (n == 0)
        printf("%d", a);
    // display first two fibonacci
    else
        printf("%d %d", a, b);

    //now calculate the remaining terms upto n
    while (a + b <= n) {
        // calculate next fibonacci
        t = a + b;
        //display next fibonacci
        printf(" %d", t);

        //assign values for next iteration
        a = b;
        b = t;
    }

    return 0;
}

Output


Case 1:

Enter the limit = 50

Fibonacci sequence upto 50: 0 1 1 2 3 5 8 13 21 34


Case 2:

Enter the limit = 1

Fibonacci sequence upto 1: 0 1 1


Case 3:

Enter the limit = 100

Fibonacci sequence upto 100: 0 1 1 2 3 5 8 13 21 34 55 89





4. C Program & output to display fibonacci sequence upto n using recursion

Code has been copied
/*****************************
    alphabetacoder.com
C program to find fibonacci 
series upto N using recursion
******************************/

#include <stdio.h>

// recursive function to display
// fibonacci sequence
void fibonacci(int a, int b, int n) {
    if (a <= n) {
        printf("%d ", a);
        fibonacci(b, a + b, n);
    }
}

int main() {
    // declare variables
    int n;

    // take input of the limit
    printf("Enter the limit = ");
    scanf("%d", & n);

    // display fibonacci upto n
    printf("Fibonacci sequence upto %d: ", n);
    // call the function
    // pass value of the first two 
    // terms and limit
    fibonacci(0, 1, n);

    return 0;
}

Output


Case 1:

Enter the limit = 50

Fibonacci sequence upto 50: 0 1 1 2 3 5 8 13 21 34


Case 2:

Enter the limit = 100

Fibonacci sequence upto 100: 0 1 1 2 3 5 8 13 21 34 55 89