Skip to content

Commit 280509f

Browse files
authored
Merge pull request #32 from timothycdykes/master
Added algorithm: calculate nth Fibonacci number
2 parents 23dd9c6 + adbf34c commit 280509f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Miscellaneous/NthFibonacci.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class NthFibonacci {
2+
3+
/*
4+
* Print the nth Fibonacci number for a given number, n
5+
* using recursion
6+
*/
7+
8+
static int fib(int n) {
9+
if (n <= 1) {
10+
return n;
11+
}
12+
return fib(n - 1) + fib(n - 2);
13+
}
14+
15+
public static void main(String[ ] args) {
16+
17+
int n = 5;
18+
System.out.println(fib(n));
19+
20+
}
21+
}

0 commit comments

Comments
 (0)