Display Fibonacci Sequence in c programming

In this example, you will learn to display the Fibonacci sequence of first n numbers (entered by the user).

What is Fibonacci Sequence?

The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.

	
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13,..., n
    

C Program to Display Fibonacci Sequence

	
#include <stdio.h>
int main() {
    int i, n, t1 = 0, t2 = 1;
    int nextTerm = t1 + t2;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    printf("Fibonacci Series: %d, %d, ", t1, t2);

    for (i = 1; i <= n; ++i) {
        printf("%d, ", nextTerm);
        t1 = t2;
        t2 = nextTerm;
        nextTerm = t1 + t2;
    }
    return 0;
}
	

Output :

	
Enter the number of terms: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
	

Share your thoughts

Ask anything about this examples