Home » C Program » Fibonacci series program in C using iteration.

Fibonacci series program in C using iteration.

Last updated:

Support this post with a reaction:

6,558 views

The Fibonacci series in C is a number sequence in which each number is calculated by adding the previous two numbers. The series generally starts with 0 and 1, followed by 1, 2, 3, 5, 8, 13, and so on.

In this program count of output elements of the series is depends upon the input given by users. Program will print n number of elements in a series, where n is a number given by the user as a input.

Before moving directly on the writing Fibonacci series in c program, first you should know

What is Fibonacci Series?

A Fibonacci series is a series in which next number is a sum of previous two numbers.

The Fibonacci series is a sequence of numbers in which every number after the first two numbers is the sum of the two preceding numbers.

The first two numbers are:

  • First number = 0
  • Second number = 1

The sequence is:

0,1,1,2,3,5,8,13,21,34,55,… 0,1,1,2,3,5,8,13,21,34,55,\ldots

Mathematical formula:

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

Where:

  • F(n) represents the current Fibonacci number.
  • F(n-1) represents the previous number.
  • F(n-2) represents the number before the previous number.

Algorithm to Generate Fibonacci Series in C

  1. Start.
  2. Declare variables n, first, second, next, and i.
  3. Initialize first = 0 and second = 1.
  4. Read the number of terms n.
  5. Repeat the following steps while i is less than n:
    • Print first.
    • Calculate next = first + second.
    • Assign second to first.
    • Assign next to second.
  6. Stop.

How our program will behave?

Suppose if someone gives an input 5 then our program should print first 5 numbers of the series.

Like if someone given 5 as a input then our Fibonacci series program which is written in C should print output as,

0, 1, 1, 2, 3

C program to print Fibonacci series using Iterative methods

#include <stdio.h>
#include <conio.h> 
int main()
{
  int n, first = 0, second = 1, result, i;
  printf("Please give an input upto you want to print series :");
  scanf("%d", &n);
  printf("Fibonacci Series is: \n");
  for (i = 0; i < n; i++)
  {
    if (i <= 1)
      result = i;
    else
    {
      result = first + second;
      first = second;
      second = result;
    }
    printf("%d \n", result);
  } 
  return 0;
}

Output 1:

Please give an input upto you want to print series :12
Fibonacci Series is: 
0 
1 
1 
2 
3 
5 
8 
13 
21 
34 
55 
89

Explanation of Fibonacci series in C program using Iterative method

  • Initialize first = 0 and second = 1.
  • Read the number of terms from the user.
  • Start a for loop that executes n times.
  • Print the value stored in first.
  • Calculate the next number using next = first + second.
  • Update first = second.
  • Update second = next.
  • Repeat until all requested terms are printed.

Dry Run of Fibonacci Series in C

Suppose the user enters 7.

IterationFirstSecondNextPrinted
10110
21121
31231
42352
53583
658135
7813218

Final output: 0 1 1 2 3 5 8

C Program to Print Fibonacci Series Using Recursion

Recursion is another method of generating the Fibonacci series. In this method, a function calls itself to calculate the Fibonacci number.

#include <stdio.h>

long long fibonacci(int n) {
    if (n == 0)
        return 0;

    if (n == 1)
        return 1;

    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int n, i;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    if (n < 0 || n > 93) {
        printf("Please enter a number between 0 and 93.");
        return 0;
    }

    printf("Fibonacci Series: ");

    for (i = 0; i < n; i++) {
        printf("%lld ", fibonacci(i));
    }

    return 0;
}

Output:

Enter the number of terms: 7

Fibonacci Series: 0 1 1 2 3 5 8

How does recursion work?

  • The function returns 0 when n == 0.
  • It returns 1 when n == 1.
  • For other values, it adds the results of fibonacci(n-1) and fibonacci(n-2).
  • The function continues calling itself until it reaches the base conditions.

Time and Space Complexity

Complexity depends on the method used to generate the Fibonacci series.

MethodTime ComplexityAuxiliary Space Complexity
Iterative (For Loop)O(n)O(1)
RecursiveO(2ⁿ)O(n)