The Fibonacci series is a sequence of numbers where each number is the sum of the two numbers before it. It starts with 0 and 1. So, it goes like this: 0, 1, 1, 2, 3, 5, 8, 13, and so on. The C program for Fibonacci series is a classic problem in coding. Writing a Fibonacci series teaches you how to work with loops, recursion, and basic algorithms.
Publication Date: 11 Dec 2024
Reading Time: 6 min read
The Fibonacci series is a sequence where each number is the sum of the two numbers that come before it. The problems usually start with 0 and 1.
In mathematics, the Fibonacci series is expressed using the recurrence relation:
F(n) = F(n-1) + F(n-2)
With the starting conditions:
The simple formula of this series can be used to build the entire sequence. Fibonacci series forms the foundation of many mathematical concepts and real world applications so learning to write a program for it is important.
This program prints the Fibonacci series using recursion. The printFib() function handles edge cases for n less than 3 and prints the first two terms for larger values. It then calls the recursive fib() function, which calculates each term as the sum of the previous two and prints them recursively.
// C Program to print the Fibonacci series using recursion
#include <stdio.h>
// Recursive function to print the fibonacci series
void fib(int n, int prev1, int prev2) {
// Base Case: when n gets less than 3
if (n < 3) {
return;
}
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
printf("%d ", curr);
return fib(n - 1, prev1, prev2);
}
// Function that handles the first two terms and calls the
// recursive function
void printFib(int n) {
// When the number of terms is less than 1
if (n < 1) {
printf("Invalid number of terms\n");
}
// When the number of terms is 1
else if (n == 1) {
printf("%d ", 0);
}
// When the number of terms is 2
else if (n == 2) {
printf("%d %d", 0, 1);
}
// When number of terms greater than 2
else {
printf("%d %d ", 0, 1);
fib(n, 0, 1);
}
return;
}
int main() {
int n = 9;
// Printing first 9 fibonacci series terms
printFib(n);
return 0;
}
0 1 1 1 2 3 5 8 13
In this program, we generate the Fibonacci series using a for loop. When you run it, the user is prompted to enter how many Fibonacci numbers to display. Say you want it to show the first 15 Fibonacci numbers, starting with 0 and 1. It prints the current number (num1) and calculates the next number by adding the previous two. The loop repeats until all terms are printed.
#include <stdio.h>
int main() {
int n, num1 = 0, num2 = 1, nextNum;
printf("Enter the number of Fibonacci numbers to generate: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 1; i <= n; ++i) {
printf("%d, ", num1);
nextNum = num1 + num2;
num1 = num2;
num2 = nextNum;
}
return 0;
}
Enter the number of Fibonacci numbers to generate: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
This C Fibonacci program generates the series using a while loop. The user just has to enter the number of terms to display in the Fibonacci series. Starting with 0 and 1, it prints the current number (num1). It calculates the next term by adding the previous two and repeats the process until the specified number of terms is printed.
#include <stdio.h>
int main() {
int n, num1 = 0, num2 = 1, nextNum, count = 0;
printf("Enter the number of Fibonacci numbers to generate: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
while (count < n) {
printf("%d, ", num1);
nextNum = num1 + num2;
num1 = num2;
num2 = nextNum;
count++;
}
return 0;
}
Enter the number of Fibonacci numbers to generate: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
In this Fibonacci C code, we can generate the series using a separate function called Fibonacci(). The function takes the number of terms (n) as input and uses a loop to calculate and print the series. The user has to input the value of n in the main() function, which then calls the Fibonacci() function to display the sequence.
#include <stdio.h>
// Function to print Fibonacci series up to n terms
void fibonacci(int n) {
int num1 = 0, num2 = 1, nextNum;
for (int i = 1; i <= n; i++) {
printf("%d ", num1);
nextNum = num1 + num2;
num1 = num2;
num2 = nextNum;
}
}
int main() {
int n;
// Get the number of terms from the user
printf("Enter the number of Fibonacci numbers to generate: ");
scanf("%d", &n);
// Call the Fibonacci function
printf("Fibonacci Series: ");
fibonacci(n);
return 0;
}
Enter the number of Fibonacci numbers to generate: 15
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
The Fibonacci series is a fundamental program that every beginner or a student in coding should learn. It introduces key concepts like loops, recursion, and functions, which form the basics for solving more complex problems.
Practising the C program for Fibonacci series in different ways helps strengthen logical thinking and coding skills. You will also learn how the same task can be approached with multiple solutions. It is a critical skill in software development.
Mastering such basics lays a strong foundation for becoming a confident and capable programmer. To learn more and get started on your professional journey, join a certification program like CCBO 4.0 Academy to build your coding skills from the ground up.
The Fibonacci series is a sequence where each number is the sum of the previous two. In C Fibonacci program, it can be calculated using loops, recursion, or functions.
The C programming fibonacci series is a basic program that teaches logic, algorithms, and concepts like recursion and iteration. Practicing it strengthens problem solving skills and helps understand different ways to approach the coding problem.
Recursion solves the Fibonacci series by calling the same function repeatedly. Each call calculates F(n) as the sum of F(n-1) and F(n-2) until given output.
Loops are faster and more memory efficient for the Fibonacci series. But recursion sticks to the mathematical definition. But it is a little slower.
The series can be extended to calculate Fibonacci terms for large values using arrays or dynamic programming.
Complete Guide on String Functions in C - Learn essential string functions in C with syntax, examples, memory rules, and safe practices. (29 Dec 2025, 5 min read)
Mastering Insertion Sort in C: Code, Logic, and Applications - Understand insertion sort in C with easy-to-follow logic, code examples, and practical tips. (29 Dec 2025, 6 min read)
Quick Sort Algorithm Explained: Steps, Code Examples and Use Cases - Learn the Quick Sort Algorithm with clear steps, partition logic, Python & C++ code examples. (28 Dec 2025, 6 min read)
Switch Statement in C: Syntax, Flowchart & Sample Programs - Learn how to use the switch statement in C programming with simple syntax and real-life examples. (27 Dec 2025, 6 min read)
The Ultimate Guide to Binary Search Algorithm in C - Learn the Binary Search Algorithm with steps, examples, and efficiency insights. (27 Dec 2025, 8 min read)
Merge Sort in C: A Step-by-Step Guide with Code Examples - Learn how Merge Sort works in C with easy-to-follow examples and step-by-step logic. (26 Dec 2025, 8 min read)
NxtWave offers comprehensive programming courses and certifications to help students and professionals build their coding skills from the ground up.