1️⃣Fibonacci Series in C (With Program and Explanation)

Fibonacci Series in C (With Program and Explanation)

Introduction

In this blog post, we will learn about the Fibonacci series and how to write a C program to print the Fibonacci series.
This topic is very important for beginners, college exams, and coding interviews.

What is Fibonacci Series?

The Fibonacci series is a sequence of numbers in which:

  • The first two numbers are 0 and 1

  • Each next number is the sum of the previous two numbers

Example:

0, 1, 1, 2, 3, 5, 8, 13, 21, ...

Formula:

Fn = F(n-1) + F(n-2)

Fibonacci Series Program in C

C Program to Print Fibonacci Series

#include <stdio.h> int main() { int n, first = 0, second = 1, next, i; printf("Enter the number of terms: "); scanf("%d", &n); printf("Fibonacci Series: "); for(i = 1; i <= n; i++) { printf("%d ", first); next = first + second; first = second; second = next; } return 0; }

Explanation of the Program

  • first stores the first Fibonacci number (0)

  • second stores the second Fibonacci number (1)

  • next stores the next term of the series

  • The for loop runs n times

  • In each iteration:

    • The current value of first is printed

    • next is calculated as first + second

    • Values are updated for the next iteration


Sample Output

Enter the number of terms: 7 Fibonacci Series: 0 1 1 2 3 5 8

Fibonacci Series Using While Loop (Optional)

#include <stdio.h> int main() { int n, i = 0, a = 0, b = 1, c; printf("Enter number of terms: "); scanf("%d", &n); while(i < n) { printf("%d ", a); c = a + b; a = b; b = c; i++; } return 0; }

Important Points

  • Fibonacci series is commonly asked in college exams

  • Helps in understanding loops and variables

  • Useful for logic building


Conclusion

In this post, we learned what the Fibonacci series is and how to write a C program to print it using a for loop and a while loop.
Practicing such programs will help you improve your C programming skills.


📌 Keep Learning!

In the next post, we will learn:
👉 C Program to Check Prime Number

Comments

Popular posts from this blog

⓮C Program to calculate Simple Interest (With Program and Explanation)

⑫ C Program to Swap Two Numbers Without Using a Temporary Variable (With Program and Explanation)

9️⃣C Program to Find LCM of Two Numbers (With Program and Explanation)