Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C Program to Display Fibonacci Series

Posted in C Tutorial
Updated on Jan 10, 2024
By Mari Selvan
πŸ‘οΈ 203 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C Program to Display Fibonacci Series

Photo Credit to CodeToFun

πŸ™‹ Introduction

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.

This series has widespread applications in various fields, including mathematics and computer science.

In this tutorial, we'll explore a C program that generates and displays the Fibonacci series.

πŸ“„ Example

Let's delve into the C code that generates and displays the Fibonacci series.

displayFibonacci.c
Copied
Copy To Clipboard
#include <stdio.h>

// Function to display Fibonacci series up to n terms
void displayFibonacci(int n) {
  int first = 0, second = 1, next;

  printf("Fibonacci series up to %d terms: ", n);

  for (int i = 0; i < n; ++i) {
    printf("%d, ", first);
    next = first + second;
    first = second;
    second = next;
  }

  printf("\n");
}

// Driver program
int main() {
  // Replace this value with the desired number of terms
  int terms = 10;

  // Call the function to display Fibonacci series
  displayFibonacci(terms);

  return 0;
}

πŸ’» Testing the Program

To test the program with a different number of terms, simply replace the value of the terms variable in the main function.

Output
Fibonacci series up to 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Compile and run the program to see the Fibonacci series up to the desired number of terms.

🧠 How the Program Works

  1. The program defines a function displayFibonacci that takes the number of terms (n) as input and prints the Fibonacci series up to n terms.
  2. Inside the function, it initializes variables first and second with 0 and 1, respectively.
  3. It then uses a loop to calculate and print the Fibonacci series up to the specified number of terms.
  4. The loop updates the values of first and second in each iteration to generate the next Fibonacci number.

🧐 Understanding the Concept of Fibonacci Series

The Fibonacci series starts with 0 and 1. Each subsequent number in the series is the sum of the two preceding ones. The series begins as follows:

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

🎒 Optimizing the Program

While the provided program is straightforward, consider exploring and implementing optimizations for larger Fibonacci series, such as using memoization techniques to avoid redundant calculations.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
7 months ago

If you have any doubts regarding this article (C Program to Display Fibonacci Series) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy