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

View: 761    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;
}```

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

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

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

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.

• ### SqlCommand to T-SQL Printer

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

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

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

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

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

File suggestion for you
• HTML-CSS template - 521 view People love naming things. And developers love acronyms. And thus here we are, finding ourselves with all of these CSS naming conventions and...

• Javascript - 354 view This puzzle has been asked. In this java puzzle, you have a series of numbers start (e.g. 1….N) and exactly one number in this series is missing. You...

• CorelDRAW Graphics - 817 view Combine (Ctrl + L): link object, the intersection of the objects will be created by the empty area.

• Codeigniter - 799 view I want that the typeahead list will show a list of names, and when I select one of the name to write the id value to a hidden field.

• HTML-CSS template - 1264 view capable of bending or being bent, able to be easily modified to respond to altered circumstances or conditions.

File suggestion for you
• HTML-CSS template - 521 view People love naming things. And developers love acronyms. And thus here we are, finding ourselves with all of these CSS naming conventions and...

• Javascript - 354 view This puzzle has been asked. In this java puzzle, you have a series of numbers start (e.g. 1….N) and exactly one number in this series is missing. You...

• CorelDRAW Graphics - 817 view Combine (Ctrl + L): link object, the intersection of the objects will be created by the empty area.

• Codeigniter - 799 view I want that the typeahead list will show a list of names, and when I select one of the name to write the id value to a hidden field.

• HTML-CSS template - 1264 view capable of bending or being bent, able to be easily modified to respond to altered circumstances or conditions.

File suggestion for you
• HTML-CSS template - 521 view People love naming things. And developers love acronyms. And thus here we are, finding ourselves with all of these CSS naming conventions and...

• Javascript - 354 view This puzzle has been asked. In this java puzzle, you have a series of numbers start (e.g. 1….N) and exactly one number in this series is missing. You...

• CorelDRAW Graphics - 817 view Combine (Ctrl + L): link object, the intersection of the objects will be created by the empty area.

• Codeigniter - 799 view I want that the typeahead list will show a list of names, and when I select one of the name to write the id value to a hidden field.

• HTML-CSS template - 1264 view capable of bending or being bent, able to be easily modified to respond to altered circumstances or conditions.

File suggestion for you
• HTML-CSS template - 521 view People love naming things. And developers love acronyms. And thus here we are, finding ourselves with all of these CSS naming conventions and...

• Javascript - 354 view This puzzle has been asked. In this java puzzle, you have a series of numbers start (e.g. 1….N) and exactly one number in this series is missing. You...

• CorelDRAW Graphics - 817 view Combine (Ctrl + L): link object, the intersection of the objects will be created by the empty area.

• Codeigniter - 799 view I want that the typeahead list will show a list of names, and when I select one of the name to write the id value to a hidden field.

• HTML-CSS template - 1264 view capable of bending or being bent, able to be easily modified to respond to altered circumstances or conditions.

Mega tags
Mega tags
Mega tags
Mega tags
• Javascript - 238 view Bing Search API is a set of REST interfaces that find web pages, news,...

• HTML-CSS template - 408 view Pinkrio is a strong and powerful XHTML/CSS template with a clean and...

• PostgreSQL - 1320 view In this article, I want to show some operations in both PostgreSQL &...

• Java mobile - 763 view If you missed the premiere of one of our virtual tech summits you'll...

• Mobile applications - 214 view In this tutorial we will try to create a Simple Image Viewer using...

• Embedded Systems - 1456 view Termie opens a serial port and logs data received to a window. It is...

• Javascript - 448 view I have tried to collect some top spring core interview questionswhich...

• Javascript - 348 view So far in previous tutorials, we have learned about securing your...

• Javascript - 666 view Working with CSV files is not common, but it may be required at times...

• Development Tools - 829 view nteractive analysis is a cycle analysis procedure of assumption,...

• Javascript - 238 view Bing Search API is a set of REST interfaces that find web pages, news,...

• HTML-CSS template - 408 view Pinkrio is a strong and powerful XHTML/CSS template with a clean and...

• PostgreSQL - 1320 view In this article, I want to show some operations in both PostgreSQL &...

• Java mobile - 763 view If you missed the premiere of one of our virtual tech summits you'll...

• Mobile applications - 214 view In this tutorial we will try to create a Simple Image Viewer using...

• Embedded Systems - 1456 view Termie opens a serial port and logs data received to a window. It is...

• Javascript - 448 view I have tried to collect some top spring core interview questionswhich...

• Javascript - 348 view So far in previous tutorials, we have learned about securing your...

• Javascript - 666 view Working with CSV files is not common, but it may be required at times...

• Development Tools - 829 view nteractive analysis is a cycle analysis procedure of assumption,...

• Javascript - 238 view Bing Search API is a set of REST interfaces that find web pages, news,...

• HTML-CSS template - 408 view Pinkrio is a strong and powerful XHTML/CSS template with a clean and...

• PostgreSQL - 1320 view In this article, I want to show some operations in both PostgreSQL &...

• Java mobile - 763 view If you missed the premiere of one of our virtual tech summits you'll...

• Mobile applications - 214 view In this tutorial we will try to create a Simple Image Viewer using...

• Embedded Systems - 1456 view Termie opens a serial port and logs data received to a window. It is...

• Javascript - 448 view I have tried to collect some top spring core interview questionswhich...

• Javascript - 348 view So far in previous tutorials, we have learned about securing your...

• Javascript - 666 view Working with CSV files is not common, but it may be required at times...

• Development Tools - 829 view nteractive analysis is a cycle analysis procedure of assumption,...

• Javascript - 238 view Bing Search API is a set of REST interfaces that find web pages, news,...

• HTML-CSS template - 408 view Pinkrio is a strong and powerful XHTML/CSS template with a clean and...

• PostgreSQL - 1320 view In this article, I want to show some operations in both PostgreSQL &...

• Java mobile - 763 view If you missed the premiere of one of our virtual tech summits you'll...

• Mobile applications - 214 view In this tutorial we will try to create a Simple Image Viewer using...

• Embedded Systems - 1456 view Termie opens a serial port and logs data received to a window. It is...

• Javascript - 448 view I have tried to collect some top spring core interview questionswhich...

• Javascript - 348 view So far in previous tutorials, we have learned about securing your...

• Javascript - 666 view Working with CSV files is not common, but it may be required at times...

• Development Tools - 829 view nteractive analysis is a cycle analysis procedure of assumption,...