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

View: 680    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

  • C# - Simple Phone Directory System

    C# - Simple Phone Directory System

    View: 6    Download: 0   Comment: 0

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

    Simple Phone Directory System Please make scan first using your antivirus. thanks.

  • School Management System

    School Management System

    View: 12    Download: 1   Comment: 0

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

    his School Management Software is Best School Management System written in C#.Net 4.0 as front end and SQL Server 2008as Back end.We can also manage Multiple branches of a School by This Software ... Requirement : Visual Studio 2010/2012/2013/2015...

  • C and C++ SDSSU compilation

    C and C++ SDSSU compilation

    View: 33    Download: 0   Comment: 0

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

    This is the compilation of my 1st year output in C and C++ programming languages. It contains some basic activity like addition, subtraction, multiplication, division and other educational information that can improve and enhance the skills of our...

  • Enrollment and Billing System in C#

    Enrollment and Billing System in C#

    View: 28    Download: 2   Comment: 0

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

    Enrollment and Billing System This is a project entitled Enrollment and Billing System written in C# and SQL Server Management Studio R2 2008 database for Christian Kiddie Care. The system has the following features: Transactions Add Student Add...

  • SqlCommand to T-SQL Printer

    SqlCommand to T-SQL Printer

    View: 291    Download: 0   Comment: 0

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

    In this article, we are going to see a log printer working with Entity Framework that converts SqlCommand to a completely executable T-SQL statement, including all parameter declarations and write to log file. Once a command is executed, You will see...

  • Windows Clipboard Formats

    Windows Clipboard Formats

    View: 123    Download: 0   Comment: 0

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

    This reference gives you an overview of standard and common clipboard formats with links to the data structures.

  • Insert 1 element at the beginning of an array managed by a pointer without moving

    Insert 1 element at the beginning of an array managed by a pointer without moving

    View: 102    Download: 0   Comment: 0

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

    Insert 1 element at the beginning of an array managed by a pointer without moving

  • An autonomous and concealed IPC mechanism

    An autonomous and concealed IPC mechanism

    View: 189    Download: 0   Comment: 0

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

    What if you have one or more modules, running independently in a system, which should collaborate between each other? Well, they will have to be able to exchange data, organized as events, messages or packets of some sort.

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