forked from NaNaDi/Programming_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
40 lines (24 loc) · 791 Bytes
/
Copy pathFibonacci.java
File metadata and controls
40 lines (24 loc) · 791 Bytes
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
import java.util.Scanner;
public class Fibonacci {
//todo: implement a recursive function which takes an integer value as inputs and outputs the corresponding Fibonacci number.
//if you don't know what Fibonacci numbers are you can read it here: https://en.wikipedia.org/wiki/Fibonacci_number
public static int fibonacci(int n){
if(n>=2){
return fibonacci(n-1) + fibonacci(n-2);
}
else if(n==1)
return 1;
else
return 0;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
Fibonacci fib = new Fibonacci();
while(true){
System.out.println("Enter a number(n): ");
int num = scan.nextInt();
int result = fib.fibonacci(num);
System.out.println("Fibonacci of n = "+result);
}
}
}