Write a program in C++ to find the nth term in the Fibonacci series using recursion. Note that the first 2 terms in the Fibonacci Series are 0 and 1.
INPUT & OUTPUT FORMAT:
Input consists of an integer.
Refer sample input and output for formatting specifications.
All text in bold corresponds to the input and the rest corresponds to output.
SAMPLE INPUT & OUTPUT:
5
The term 5 in the fibonacci series is 3
Answers
Answer:
#include<iostream>
using namespace std;
int fib(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{
return(fib(n-1)+fib(n-2));
}
}
int main()
{
int n,i=0;
cout<<"number of terms for Fibonacci Series:";
cin>>n;
cout<<"\nFibonacci Series :\n";
while(i<n)
{
cout<<" "<<fib(i);
i++;
}
return 0;
}
Explanation:
Answer:Answer:
#include<iostream>
using namespace std;
int fib(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{
return(fib(n-1)+fib(n-2));
}
}
int main()
{
int n,i=0;
cout<<"number of terms for Fibonacci Series:";
cin>>n;
cout<<"\nFibonacci Series :\n";
while(i<n)
{
cout<<" "<<fib(i);
i++;
}
return 0;
}
Explanation:
Explanation: