C/C++ Program for Fibonacci Series Using Recursion

Series 0, 1, 1, 2, 3, 5, 8, 13, 21 . . . . . . . is a Fibonacci series. In Fibonacci series, each term is the sum of the two preceding terms.


The C and C++ program for Fibonacci series using recursion is given below.

C Program

C++ Program



C/C++ Program for Fibonacci Series Using Recursion

12 thoughts on “C/C++ Program for Fibonacci Series Using Recursion

  1. Anja

    i guess 0 should not have been a part of the series….
    so in the function u should have used return fibbonacci(n)+fibbonacci(n-1)
    please correct me if i am wrong

    1. kc

      For not getting 0 in the output,
      i can be initialized as 1 instead of 0 : int i=1;

  2. Anthonick

    The while statement needs to be, while(i <= n)(line 24), and (int i = 0) needs to be initialized at 1(line 19), not at 0. Changing this will result in the proper value for any fibonacci(n). As is right now, it is giving you the value at fibonacci(n-1), hence the reason fibonacci(8) yields a value of 13.

  3. Doe

    Can someone help me with this, write a recursive program to implement the Fibonacci series from 0 to 21.

    1. Sam


      using namespace std;

      int Fib(int n);

      int main() {
      int i;

      for(i=1;i<=20;i++) {
      cout << "Fib(" << i << ") = " << Fib(i) << endl;

      return 0;

      int Fib(int n) {
      int returnValue;

      // handle the base case first;
      if (n == 1 or n == 2) {
      returnValue = 1;
      } else {
      returnValue = Fib(n-1) + Fib(n-2);
      return returnValue;

  4. negashbezu

    i relly appreciet you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  5. vasudevareddy

    i need to get the output as, if i enter 15 as input then i should get the answer as 0,1,1,2,3,5,8,13.so, where should i have to change the code?

  6. mania

    hi. if we want just the last number of this, what should we do ?
    we give : 3
    we get : 2

    1. P.L

      Give me please more and more information about fibonacci. I want to learn C++ very well


Leave a Reply

Your email address will not be published. Required fields are marked *