Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
JavaScript Program to print Fibonacci Series
Next article icon

C Program to Print Fibonacci Series

Last Updated : 03 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers are 0 and 1 which are used to generate the whole series.

Example

Input: n = 5
Output: 0 1 1 2 3
Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.

Input: N = 7
Output: 0 1 1 2 3 5 8
Explanation: The first 7 terms of the Fibonacci series are 0, 1, 1, 2, 3, 5, 8.

In this article, we will learn how to print the Fibonacci series upto given number of Terms

Table of Content

  • Print Fibonacci Series Using Loops
  • Print Fibonacci Series Using Recursion

There are two major ways to compute and print the Fibonacci series in C:

Print Fibonacci Series Using Loops

We can use one of the C loops to iterate and print the given number of terms. The first two terms, F1 and F2 should be handled separately. After that, we can use two variables to store the previous two terms and print the current term by adding these two. We have to keep updating the previous terms as we move to print the next term in the series.

Program to Print Fibonacci Series Upto n Terms

C
// C Program to print the fibonacci series using loops #include <stdio.h>  void printFib(int n) {      	// If the number of terms is smaller than 1     if (n < 1) {         printf("Invalid Number of terms\n");         return;     }      	// First two terms of the series     int prev1 = 1;     int prev2 = 0;      // for loop that prints n terms of fibonacci series     for (int i = 1; i <= n; i++) {       	       	// Print current term and update previous terms         if (i > 2) {             int curr = prev1 + prev2;             prev2 = prev1;             prev1 = curr;             printf("%d ", curr);         } 		else if (i == 1)             printf("%d ", prev2);         else if (i == 2)             printf("%d ", prev1);     } }  int main() {     int n = 9;      	// Printing first n fibonacci terms     printFib(n);     return 0; } 

Output
0 1 1 2 3 5 8 13 21  

Time Complexity: O(n), where n is the number of terms to be printed.
Space Complexity: O(1).

Print Fibonacci Series Using Recursion

We can also print Fibonacci series using recursion. This method is as much as popular as iteration method.

We will use a function that prints the first two terms, and then call the recursive function that handles the rest of the terms. In recursive function, we pass the previous two terms

Program to Print Fibonacci Series Upto n Terms Using Recursion

C
#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;     printf("%d ", curr);        return fib(n - 1, prev2, curr); }  // 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; } 

Output
0 1 1 2 3 5 8 13 21 

Time complexity: O(n), where n is the number of terms to be printed.
Space Complexity: O(n) for recursive stack space.



Next Article
JavaScript Program to print Fibonacci Series
author
kartik
Improve
Article Tags :
  • C Language
  • C Programs
  • C Basic Programs

Similar Reads

  • How to check if a given number is Fibonacci number?
    Given a number ‘n’, how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .. Examples :Input : 8Output : YesInput : 34Output : YesInput : 41Output : NoApproach 1:A simple way is to generate Fibonacci numbers until the generated number
    15+ min read
  • Nth Fibonacci Number
    Given a positive integer n, the task is to find the nth Fibonacci number. 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, 21 Example: Inp
    15+ min read
  • C++ Program For Fibonacci Numbers
    The Fibonacci series is the sequence where each number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1 and are used to generate the whole series. In this article, we will learn how to find the nth Fibonacci number in C++. Examples Input: 5Output: 5Ex
    5 min read
  • Python Program for n-th Fibonacci number
    In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2With seed values F0 = 0 and F1 = 1.Table of Content Python Program for n-th Fibonacci number Using Formula Python Program for n-th Fibonacci number Using RecursionPython Program for n-th
    6 min read
  • Interesting Programming facts about Fibonacci numbers
    We know Fibonacci number, Fn = Fn-1 + Fn-2. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, .... . Here are some interesting facts about Fibonacci number : 1. Pattern in Last digits of Fibonacci numbers : Last digits of first few Fibonacci Numbers ar
    15+ min read
  • Find nth Fibonacci number using Golden ratio
    Fibonacci series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ........Different methods to find nth Fibonacci number are already discussed. Another simple way of finding nth Fibonacci number is using golden ratio as Fibonacci numbers maintain approximate golden ratio till infinite. Golden ratio: [Tex]\varphi
    6 min read
  • Fast Doubling method to find the Nth Fibonacci number
    Given an integer N, the task is to find the N-th Fibonacci numbers.Examples: Input: N = 3 Output: 2 Explanation: F(1) = 1, F(2) = 1 F(3) = F(1) + F(2) = 2 Input: N = 6 Output: 8 Approach: The Matrix Exponentiation Method is already discussed before. The Doubling Method can be seen as an improvement
    14 min read
  • Tail Recursion for Fibonacci
    Write a tail recursive function for calculating the n-th Fibonacci number. Examples : Input : n = 4 Output : fib(4) = 3 Input : n = 9 Output : fib(9) = 34 Prerequisites : Tail Recursion, Fibonacci numbersA recursive function is tail recursive when the recursive call is the last thing executed by the
    4 min read
  • Sum of Fibonacci Numbers
    Given a number positive number n, find value of f0 + f1 + f2 + .... + fn where fi indicates i'th Fibonacci number. Remember that f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, f5 = 5, ... Examples : Input : n = 3Output : 4Explanation : 0 + 1 + 1 + 2 = 4 Input : n = 4Output : 7Explanation : 0 + 1 + 1 + 2 +
    9 min read
  • Fibonacci Series

    • Program to Print Fibonacci Series
      Ever wondered about the cool math behind the Fibonacci series? This simple pattern has a remarkable presence in nature, from the arrangement of leaves on plants to the spirals of seashells. We're diving into this Fibonacci Series sequence. It's not just math, it's in art, nature, and more! Let's dis
      9 min read

    • Program to Print Fibonacci Series in Java
      The Fibonacci series is a series of elements where the previous two elements are added to generate the next term. It starts with 0 and 1, for example, 0, 1, 1, 2, 3, and so on. We can mathematically represent it in the form of a function to generate the n'th Fibonacci number because it follows a con
      6 min read

    • Print the Fibonacci sequence - Python
      To print the Fibonacci sequence in Python, we need to generate a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The Fibonacci sequence follows a specific pattern that begins with 0 and 1, and every subsequent number is the sum of the two previous num
      5 min read

    • C Program to Print Fibonacci Series
      The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers are 0 and 1 which are used to generate the whole series. Example Input: n = 5Output: 0 1 1 2 3Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.
      4 min read

    • JavaScript Program to print Fibonacci Series
      The Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms. The recurrence relation defines the sequence Fn of Fibonacci numbers: Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1 Examples: Input :
      4 min read

    • Length of longest subsequence of Fibonacci Numbers in an Array
      Given an array arr containing non-negative integers, the task is to print the length of the longest subsequence of Fibonacci numbers in this array.Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 3 Here, the subsequence is {3, 2, 21} and hence the answer is 3.Input: arr[] = { 6, 4, 10, 13, 9,
      5 min read

    • Last digit of sum of numbers in the given range in the Fibonacci series
      Given two non-negative integers M, N which signifies the range [M, N] where M ? N, the task is to find the last digit of the sum of FM + FM+1... + FN where FK is the Kth Fibonacci number in the Fibonacci series. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... Examples: Input: M = 3, N = 9 Output:
      5 min read

    • K- Fibonacci series
      Given integers 'K' and 'N', the task is to find the Nth term of the K-Fibonacci series. In K - Fibonacci series, the first 'K' terms will be '1' and after that every ith term of the series will be the sum of previous 'K' elements in the same series. Examples: Input: N = 4, K = 2 Output: 3 The K-Fibo
      7 min read

    • Fibonacci Series in Bash
      Prerequisite: Fibonacci Series Write a program to print the Fibonacci sequence up to nth digit using Bash. Examples: Input : 5 Output : Fibonacci Series is : 0 1 1 2 3 Input :4 Output : Fibonacci Series is : 0 1 1 2 The Fibonacci numbers are the numbers in the following integer sequence . 0, 1, 1, 2
      1 min read

    • R Program to Print the Fibonacci Sequence
      The Fibonacci sequence is a series of numbers in which each number (known as a Fibonacci number) is the sum of the two preceding ones. The sequence starts with 0 and 1, and then each subsequent number is the sum of the two previous numbers. The Fibonacci sequence has many applications in various fie
      3 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences