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

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

9 point/3 review File has been tested

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++

How to Print Fibonacci Series using Recursion in C, C++ Posted on 28-09-2016  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. 3/10 553

Comment:

To comment you must be logged in members.

Files with category

  • An autonomous and concealed IPC mechanism

    An autonomous and concealed IPC mechanism

    View: 38    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.

  • How to Swap Two Numbers using Call by Reference in C

    How to Swap Two Numbers using Call by Reference in C

    View: 551    Download: 0   Comment: 0

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

    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

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

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

    View: 548    Download: 0   Comment: 0

    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.

  • How to Implicitly Typed Arrays In C#

    How to Implicitly Typed Arrays In C#

    View: 617    Download: 0   Comment: 0

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

    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    How to C# Static vs Instance Methods As Event Handlers

    View: 423    Download: 0   Comment: 0

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

    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    Build Pass By Reference To Method In C#

    View: 578    Download: 0   Comment: 0

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

    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    How to C# Continue Statement

    View: 395    Download: 0   Comment: 0

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

    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    How to Single Dimensional Arrays In C#

    View: 344    Download: 0   Comment: 0

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

    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

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