In fibonacci series, each number is the sum of the two preceding numbers.
For Example : 0, 1, 1, 2, 3, 5, 8, …
The following program returns the nth number entered by user residing in the fibonacci series.
Here is the source code of the Java program to print the nth number of a fibonacci number.
Code :
public class Fibonacci {public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the nth number in the fibonacci series");
int n = scanner.nextInt();
System.out.println("The "+n+"th no of fibonacci series is "+ fibo(9));
}
private static int fibo(int n) {
if (n <= 1) {
return n;
}
return (fibo(n - 1) + fibo(n - 2));
}
}
Ouput :
Enter the nth number in the fibonacci series
9
The 9th no of fibonacci series is 34