forked from zeel-codder/Recursion-Hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciNumber.java
47 lines (35 loc) · 978 Bytes
/
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
// package Code;
/**
* Contributor🎅
* Name: Zeel-Codder
* Github:https://github.com/zeel-codder
* WebSite(optional):https://zeelcodder.tech/
*/
/**
* 👉 Problem: Nth FibonacciNumber
* 👑 Description: Find the Nth Fibonacci Number.
* 🎓 Explanation(optional):Any fibonacci number is give by sum of last two fibonacci number
* before the number.
* Ex.
* F(N): 0 1 1 2 3 5 8 13 21...
* N : 0 1 2 3 4 5 6 7 8....
* f(n)={
* 0 for n==0
* 1 for n==1
* f(n-1)+f(n-2) otherwise
* }
*/
public class FibonacciNumber {
// Main Function
public static void main(String[] args) {
int f=NthFibonacciNumber(5);
System.out.println(f);
}
// find Nth FibonacciNumber
static int NthFibonacciNumber(int n){
if(n==0) return 0;
if(n==1) return 1;
//Recursive Call
return NthFibonacciNumber(n-1)+NthFibonacciNumber(n-2);
}
}