Skip to content

Commit adbf34c

Browse files
Added algorithm: calculate nth Fibonacci number
Added an algorithm for calculating the nth Fibonacci number using a recursive method.
1 parent 9ef7243 commit adbf34c

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)