Loading...

How to Print Fibonacci Series using Recursion in C, C++

View: 708    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

You can check my previous post to use iterative approach to print Fibonacci series.

What is Recursion.

In recursion function call itself until the base condition is reached.
Read explanation of recursion and what’s the difference between iterative and recursive function.

Objective Question on Recursion.

Fibonacci Series

It is the series of numbers, such that the next number is the sum of previous two numbers.

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

In Fibonacci series first two numbers is 0 and 1. Next number is the sum of first two numbers.

0 + 1 = 1

1 + 1 = 2

2 + 1 = 3

3 + 2 = 5

Print Fibonacci Series using Recursion in C

Now you have learned what is fibonacci series. Let’s write a code using recursion to generate fibonacci series.

#include <stdio.h>
 
int fibonacci(int num){
 
    if(num==0){
 
       return 0;
 
    }else if(num==1){
 
       return 1;
 
    }  else {
 
        return (fibonacci(num-1) + fibonacci(num-2));
    } 
 
}
 
main()
{
 
   /* Assume we want to print first six number of fibonacci series. */
 
   int num = 6,i;
 
  for(i = 0; i < num; i++) {
 
     /* Call fibonacci function. */
 
      printf(" %d ",fibonacci(i));
 
  }
}

Program Logic

1. Return 0 if the input number is 0, 1 if the input number is 1.

2. If number is greater than 1 then return func(n-1) + func(n-2)func is the name of function.

Let’s explain this, suppose num is 4. As per fibonacci function last else condition is satisfied.

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

/* When 4 is input, as per else condition. */
 
 fibonacci(3) + fibonacci(2)
 
 /* fibonacci(3) and fibonacci(2), calls function */
 
 (fibonacci(1) + fibonacci (2)) + (fibonacci(0) + fibonacci(1))
 
 1 + (fibonacci (1) + fibonacci (0) ) + 0 + 1
 
 1 + 1 + 1
 
 3

Fourth element of fibonacci series is 3.

Print Fibonacci Series using Recursion in C++

#include <iostream>
using namespace std;
 
 
int fibonacci (int num) {
 
 if ( num == 0 ) {
 
 return 0;
 
 } else if ( num == 1 ) {
 
 return 1;
 
 } else {
 
 return fibonacci (num - 1) + fibonacci (num - 2);
 }
 
 
}
 
int main() {
 
 int num;
 
 cout << " Enter a number (Positive number only) "; cin >> num;
 
 for (int i = 0 ; i < num; i++) {
 
 cout << fibonacci(i) << " ";
 }
 
 return 0;
}

How to Print Fibonacci Series using Recursion in C, C++

Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

Posted on 28-09-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Sound Player in Visual Studio C++ .NET

    Sound Player in Visual Studio C++ .NET

    View: 13    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This sound player project use the libZPlay from http://libzplay.sourceforge.net . Methods are called from library libzplay.dll with [InteropServices::DllImport("")]. The use of the library is relatively easy.

  • Quiz Management System in C++

    Quiz Management System in C++

    View: 19    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Hello World...!!! Though I am not an expert programmer but today i want to share my little knowledge of C/C++ programming by sharing this simple Quiz Management System with you people. I hope this may help some students in their mini...

  • General Quiz

    General Quiz

    View: 104    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is one of my first WPF projects, and it may be used as a template for a quiz test. The code can be improved but I think its useful for the ones who don't have experience with WPF.

  • Calculating Average and Grade

    Calculating Average and Grade

    View: 34    Download: 2   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Calculating Average and Grade is a small program in order to find the result of students in the class.

  • H.K.M.S SUPREME TYRES

    H.K.M.S SUPREME TYRES

    View: 25    Download: 1   Comment: 0

    Category: C / C++ / MFC     Fields: none

    H.K.M.S SUPREME TYRES --------------------- Goals: --------- * Brand Master * Size Master * User Master * Customer Master * Purchase Form * Order Form * Bill Form Software is as follows: ------------------------------ * Microsoft Visual Studio 2013 *...

  • Cronpp - a C++ library for CRON expressions

    Cronpp - a C++ library for CRON expressions

    View: 18    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    A CRON expression is a string composed of six fields (in some implementation seven), separated by a whites space, representing a time schedule. CRON expressions are used in various job schedulers (such as the Linux job scheduler, the Quartz...

  • Departmental store management system

    Departmental store management system

    View: 25    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is a simple project on departmental store management system developed in c++ programming language . This project helps to keep all the information details of the items available in departmental store and maintains the stock and prints the bill....

  • Volume of a Cone in C

    Volume of a Cone in C

    View: 37    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Code Features: 1. Find the Volume of a cone 2. Find the value of other properties of the cone 3. Teach user how to compute the volume of the cone manually Thanks to following for the code: John Carlo C. Nañoz, Dondon Bagasbas, Gabby Peñones

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd