forked from abhishekdoifode1/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciNumber.java
52 lines (36 loc) · 1.4 KB
/
FibonacciNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
# The Fibonacci Sequence
The Fibonacci sequence appears in nature all around us, in the arrangement of seeds in a sunflower and the spiral of a nautilus for example.
The Fibonacci sequence begins with and as its first and second terms. After these first two elements, each subsequent element is equal to the sum of the previous two elements.
Programmatically:
Given , return the number in the sequence.
As an example, . The Fibonacci sequence to is . With zero-based indexing, .
Function Description
Complete the recursive function in the editor below. It must return the element in the Fibonacci sequence.
fibonacci has the following parameter(s):
n: the integer index of the sequence to return
Input Format
The input line contains a single integer.
Output Format
Locked stub code in the editor prints the integer value returned by the function.
Sample Input
3
Sample Output
2
*/
import java.util.*;
public class Solution {
public static int fibonacci(int n) {
if(n < 1)
return 0;
if(n == 1)
return 1;
return fibonacci(n-1) + fibonacci(n-2);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
System.out.println(fibonacci(n));
}
}