Java Recursion Program Practise 2 - Fibonacci Series
Java program to find the given term of Fibonacci Series using Recursion Function
import java.util.*;
class Main {
int fib(int n){
if(n==1){
return 0;
}
if(n==2){
return 1;
}
if(n>2){
return(fib(n-2)+fib(n-1));
}
else
return -1;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a;
System.out.println("Enter the number of terms of Fibonacci series to be displayed using Recursion function");
a = in.nextInt();
Main ob = new Main();
System.out.println(ob.fib(a));
}
}
Comments
Post a Comment