The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, for example, 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. The first two numbers in the Fibonacci sequence are 0 and 1.
There are two ways to write a Fibonacci sequence program in C language:
-
Fibonacci Sequence without Recursion
-
Fibonacci Sequence with Recursion
👇Click to Claim👇
👉Collection of C Language Knowledge Materials
Fibonacci Sequence without Recursion
Below is a program written in C language to generate the Fibonacci sequence without using recursion.
#include<stdio.h>
int main() { int n1=0, n2=1, n3, i, number; printf("Please enter the number of Fibonacci sequence items to output:"); scanf("%d", &number); printf("%d %d", n1, n2); // Print 0 and 1
for(i=2; i<number; %d",="" +="" ++i)="" 0="" 0;}
Output:
Enter the number of Fibonacci sequence items to output: 150 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Fibonacci Sequence with Recursion
Below is a program written in C language to generate the Fibonacci sequence using recursion.
#include<stdio.h>
void printFibonacci(int n) { static int n1=0, n2=1, n3; if(n>0) { n3 = n1 + n2; n1 = n2; n2 = n3; printf("%d ", n3); printFibonacci(n-1); }}
int main() { int n; printf("Please enter the number of Fibonacci sequence items to output: "); scanf("%d", &n); printf("Fibonacci sequence: "); printf("%d %d ", 0, 1); printFibonacci(n-2); // n-2 because the first two numbers have already been printed return 0;}
Output:
Enter the number of Fibonacci sequence items to output: 150 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Recommended Popular Articles
-
C Language Tutorial – Detailed Explanation of the Differences Between typedef and #define in C
-
C Language Algorithm – “Solving Sudoku” Algorithm Problem
-
C++ Tutorial – Detailed Explanation of the History of C++ Language?