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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<stdio.h> int fibonacci(int n) { if((n==1)||(n==0)) { return(n); } else { return(fibonacci(n-1)+fibonacci(n-2)); } } int main() { int n,i=0; printf("Input the number of terms for Fibonacci Series:"); scanf("%d",&n); printf("\nFibonnaci Series is as follows\n"); while(i<n) { printf("%d ",fibonacci(i)); i++; } return 0; } |

### C++ Program

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include<iostream> using namespace std; int fibonacci(int n) { if((n==1)||(n==0)) { return(n); } else { return(fibonacci(n-1)+fibonacci(n-2)); } } int main() { int n,i=0; cout<<"Input the number of terms for Fibonacci Series:"; cin>>n; cout<<"\nFibonacci Series is as follows\n"; while(i<n) { cout<<" "<<fibonacci(i); i++; } return 0; } |

**Output:**

Anjai 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

kcFor not getting 0 in the output,

i can be initialized as 1 instead of 0 : int i=1;

francisWrite a recursive program for implementing a Fibonacci of any

number

AnthonickThe 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.

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

Sam#include

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;

}

AbelCan you help me with this, write

a recursive program to implement the

Fibonacci series a given number?

andualem#include

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;

}

sm. shawon srowerthis is very helpful

thanks

pankajso nice ans

negashbezui relly appreciet you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

vasudevareddyi 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?

maniahi. if we want just the last number of this, what should we do ?

we give : 3

we get : 2

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

yashi dont know programming

Jeric WokI love books!! <3 xbbb hehehehe!!

ShashiKNot actual Recursion. Its while loop which does the required part and not the method. Good to move the logic inside the method

void Recursion::FibonaciiSeries(int num)

{

static int print = 1;

static int next = 2;

if (num == 0)

{

return;

}

else

{

cout << print << " ";

int temp = print;

print = next;

next = print + temp;

FibonaciiSeries(num – 1);

}

}

p.sakaniyaif you put 50 that is not work. could you check am i right?