Problem statement:- Program to Find the nth term in the Fibonacci series using Recursion.
Data requirement:-
Input Data:- n
Output Data:-NthFibonacciNumber(n)
Output Data:-NthFibonacciNumber(n)
Example:
Input: n=3
Output:2 //(1,1,2)
Input: n=6
Output:8 //(1,1,2,3,5,8)
Program in C
Here is the source code of the C Program to Find the nth term in the Fibonacci series using Recursion.
Code:
#include<stdio.h>
int NthFibonacciNumber(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return NthFibonacciNumber(n-1)+NthFibonacciNumber(n-2);
}
int main()
{
int n;
printf("Enter the N value:");
scanf("%d",&n);
printf("Nth Fibonacci Number is: %d",NthFibonacciNumber(n));
}
Enter the N value:3
Nth Fibonacci Number is: 2
Program in C++
Here is the source code of the C++ Program to Find the nth term in the Fibonacci series using Recursion.
Code:
#include<iostream>
using namespace std;
int NthFibonacciNumber(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return NthFibonacciNumber(n-1)+NthFibonacciNumber(n-2);
}
int main()
{
int n;
cout<<"Enter the N value:";
cin>>n;
cout<<"Nth Fibonacci Number is: "<<NthFibonacciNumber(n);
}
Enter the N value:6
Nth Fibonacci Number is: 8
Program in Java
Here is the source code of the Java Program to Find the nth term in the Fibonacci series using Recursion.
Code:
import java.util.Scanner;
public class NthFibonacciValue {
static int NthFibonacciNumber(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return NthFibonacciNumber(n-1)+NthFibonacciNumber(n-2);
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n;
System.out.print("Enter the N value:");
n=cs.nextInt();
System.out.print("Nth Fibonacci Number is: "+NthFibonacciNumber(n));
cs.close();
}
}
Enter the N value:11
Nth Fibonacci Number is: 89
Program in Python
Here is the source code of the Python program to Find the nth term in the Fibonacci series using Recursion.
Code:
def NthFibonacciNumber(n):
if n==0:
return 0
elif(n==1):
return 1
else:
return NthFibonacciNumber(n-1)+NthFibonacciNumber(n-2)
n=int(input("Enter the N value:"))
print("Nth Fibonacci Number is:",NthFibonacciNumber(n))
Enter the N value:19
Nth Fibonacci Number is: 4181
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box