-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion-fibonacci-numbers.java
More file actions
52 lines (33 loc) · 1.06 KB
/
recursion-fibonacci-numbers.java
File metadata and controls
52 lines (33 loc) · 1.06 KB
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 begins with and as its respective first and second terms. After these first two elements, each subsequent element is equal to the sum of the previous two elements.
Here is the basic information you need to calculate :
Task
Given , complete the fibonacci function so it returns .
Input Format
Locked stub code in the editor reads a single integer denoting the value of and passes it to the fibonacci function.
Constraints
Output Format
Locked stub code in the editor prints the value of returned by the fibonacci function.
Sample Input
3
Sample Output
2
*/
import java.util.*;
public class Solution {
public static int fibonacci(int n) {
if (n == 0) {
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));
}
}