The Fibonacci sequence is defined by the following rules: The first two values in the sequence are 1 and 1. Every subsequent values is the sum of the two values preceding it.Write a java program that uses recursive functions to print the nth value in the Fibonacci sequence. 

Program Code:
import java.io.*;
import java.math.*;
class Fibonacci
{
int Fib(int n)
{
if(n==0)
return 0;
else if(n==1) return 1;
else
return Fib(n-1)+Fib(n-2);
}
}
class Fibrec
{
public static void main(String[] args) throws Exception
{
int n;
long r=0;
System.out.println("Enter N value:");
DataInputStream dis=new DataInputStream(System.in);
n=Integer.parseInt(dis.readLine());
Fibonacci f=new Fibonacci();
for(int i=1;i<=n; i++)
{
System.out.print(f.Fib(i)+"\t");
r=f.Fib(n);
}
System.out.println("\nThe "+n+" the value in the Fibonacci Series : "+r);
}
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: