//How to print the nth number of a Fibonacci series by recursive method
// fibo(5)
// fibo(4) + f ibo(3)
// fibo(3) + f ibo(2) + fibo(2) + fibo(1)
//fibo(2) + fibo(1) + 1 + 1 + 1
// 1 + 1 + 1 + 1 + 1 = 5
import java.io.*;
class fibonacci
{
public static long fibonacci(int n)
{
if(n<=0)
return 1;
return fibonacci(n-1) +fibonacci(n-2);
}
public static void main(String[] args) throws IOException
{
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter number : ");
int n=Integer.parseInt(rd.readLine());
System.out.print("\n" + n + "dth number of a Fibonacci series is = " + fibonacci(n));
}
}
// fibo(5)
// fibo(4) + f ibo(3)
// fibo(3) + f ibo(2) + fibo(2) + fibo(1)
//fibo(2) + fibo(1) + 1 + 1 + 1
// 1 + 1 + 1 + 1 + 1 = 5
import java.io.*;
class fibonacci
{
public static long fibonacci(int n)
{
if(n<=0)
return 1;
return fibonacci(n-1) +fibonacci(n-2);
}
public static void main(String[] args) throws IOException
{
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter number : ");
int n=Integer.parseInt(rd.readLine());
System.out.print("\n" + n + "dth number of a Fibonacci series is = " + fibonacci(n));
}
}
No comments:
Post a Comment