Fibonacci series in C

05 min read

C program to display Fibonacci series is given below. Fibonacci series is a series in which every number after the first two numbers is the sum of the two preceding ones. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

Example : 

1) 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . . . 

2) 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . .


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



Output

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


C++ program

#include <iostream>
using namespace std;
int main()
{
    int i, n, t1 = 0, t2 = 1, nextTerm=0;
    cout << "Enter the n value: ";
    cin >> n;
    cout << "Fibonacci Series: " << t1 << " " << t2 << " ";
    nextTerm = t1 + t2;
    for (i = 3; i <= n; ++i)
    {
        cout << nextTerm << " ";
        t1 = t2;
        t2 = nextTerm;
        nextTerm = t1 + t2;
    }
    return 0;
}

 

Java program

import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int i, n, t1 = 0, t2 = 1, nextTerm=0;
        System.out.println("Enter the n value: ");
        n = sc.nextInt();
        System.out.print("Fibonacci Series: " + t1 + " " + t2 + " ");
        nextTerm = t1 + t2;
        for (i = 3; i <= n; ++i)
        {
            System.out.print(nextTerm + " ");
            t1 = t2;
            t2 = nextTerm;
            nextTerm = t1 + t2;
        }
    }
}

 

Recursive method

C program

#include<stdio.h>
int Fibonacci(int n)
{
  if (n <= 1)
    return n;
  else
    return (Fibonacci(n-1) + Fibonacci(n-2));
}
int main()
{
  int n, i = 0, c;
  printf("Enter the n value: ");
  scanf("%d", &n);
  printf("Fibonacci series: ");
  for (c = 1; c <= n; c++)
  {
    printf("%d ", Fibonacci(i));
    i++;
  }
  return 0;
}

 

C++ program

#include<iostream>
using namespace std;
int Fibonacci(int n)
{
  if (n <= 1)
    return n;
  else
    return (Fibonacci(n-1) + Fibonacci(n-2));
}
int main()
{
  int n, i = 0, c;
  cout << "Enter the n value: ";
  cin >> n;
  cout << "Fibonacci series: ";
  for (c = 1; c <= n; c++)
  {
    cout << Fibonacci(i) << " ";
    i++;
  }
  return 0;
}

 

Java program

import java.util.Scanner;
public class Main
{
    public static int Fibonacci(int n)
    {
      if (n <= 1)
        return n;
      else
        return (Fibonacci(n-1) + Fibonacci(n-2));
    }
    public static void main(String args[])
    {
      Scanner sc = new Scanner(System.in);
      int n, i = 0, c;
      System.out.println("Enter the n value: ");
      n = sc.nextInt();
      System.out.println("Fibonacci series: ");
      for (c = 1; c <= n; c++)
      {
        System.out.print(Fibonacci(i) + " ");
        i++;
      }
    }
}

 

Time Complexity: O(n)

 
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error