Showing posts with label fibonacci. Show all posts
Showing posts with label fibonacci. Show all posts

Tuesday, June 16, 2020

Write a program to find the nth term in the Fibonacci series using recursion

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

SAMPLE INPUT & OUTPUT:

15

The term 5 in the fibonacci series is 377

SAMPLE INPUT & OUTPUT:

8

The term 5 in the fibonacci series is 13

SOLUTION

#include<iostream>
using namespace std;
int fibonacci(int n){
  int a[n];
  a[0] = 0;
  a[1] = 1;
  for(int i = 2; i < n; i++){
    a[i] = a[i-1] + a[i-2];
  }
  return a[n-1];
}
int main()
{
  int n;
  cin>>n;
  cout<<"The term "<<n<<" in the fibonacci series is "<<fibonacci(n);
  return 0;

}

horizontal ads