Print Fibonacci Series in C Language

To print Fibonacci Series until a specified number in C programming, take two variables n1 and n2 with 0 and 1 respectively, and in a While Loop update n1 with n2, and n2 with n1 + n2, until the specified number. During each iteration of the While Loop we get the next element of Fibonacci Series.

In this tutorial, we will write a C Program, using which we read a number n from user and print all the Fibonacci Series until this number.

C Program

In the following program, we read an integer into n from user, and print Fibonacci Series until this number.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n, n1 = 0, n2 = 1;
    
    printf("Enter a number : ");
    scanf("%d", &n);
    printf("Fibonacci Series : %d  ", n1);
    
    while (n2 < n) {
        printf("%d  ", n2);
        int temp = n1 + n2;
        n1 = n2;
        n2 = temp;
    }
    
    printf("\n");
    return 0;
}

Output

Enter a number : 10
Fibonacci Series : 0  1  1  2  3  5  8  
Program ended with exit code: 0

Output

Enter a number : 100
Fibonacci Series : 0  1  1  2  3  5  8  13  21  34  55  89  
Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned how to print Fibonacci Series until given number in C language, with examples.