Skip to content

Commit 33606aa

Browse files
shikherjaiswalmohitkyadav
authored andcommitted
Fibonacci Number [Java] (#624)
* added java program for nth fibonacci no * Added spacing between semicolon in for loop * Fixed base case for n=2 * Fixed indentation at line no 23 * Fixed indentation at line no 28 * Fixed indentation from line 24 to 29 * added alternative code for base case and fixed indentation
1 parent a7f3ee6 commit 33606aa

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
3838
| [Longest Palindromic Substring](http://www.geeksforgeeks.org/longest-palindrome-substring-set-1/) | | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.cpp) | [:white_check_mark:](longest_palindromic_substring/LongestPalindromicSubstring.java) | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.py) | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.go) | [:white_check_mark:](longest_palindromic_substring/longestPalindromicSubstring.js) | |
3939
| [Merge Sort](https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/overview-of-merge-sort) | [:white_check_mark:](merge_sort/merge_sort.c) | | [:white_check_mark:](merge_sort/MergeSort.java) | [:white_check_mark:](merge_sort/merge_sort.py) | [:white_check_mark:](merge_sort/merge_sort.go) | [:white_check_mark:](merge_sort/mergeSort.js) | [:white_check_mark:](merge_sort/MergeSort.cs) |
4040
| [Modular Exponential](http://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/) | [:white_check_mark:](modular_exponential/modular_exponential.c) | | [:white_check_mark:](modular_exponential/ModularExponential.java) | [:white_check_mark:](modular_exponential/modular_exponential.py) | [:white_check_mark:](modular_exponential/modular_exponential.go) | [:white_check_mark:](modular_exponential/modularExponential.js) | |
41+
| [Nth Fibonacci Number](https://en.wikipedia.org/wiki/Fibonacci_number) | | | [:white_check_mark:](fibonacci_number/FibonacciNumber.java) | | | | |
4142
| [N-Queen Problem](https://en.wikipedia.org/wiki/Eight_queens_puzzle) | | [:white_check_mark:](n_queen_problem/NQueenProblem.cpp) | [:white_check_mark:](n_queen_problem/NQueenProblem.java) | [:white_check_mark:](n_queen_problem/n_queen_problem.py) | [:white_check_mark:](n_queen_problem/n_queen_problem.go) | | |
4243
| [Prime Factor](https://en.wikipedia.org/wiki/Prime_factor) | [:white_check_mark:](prime_factor/prime_factor.c) | | [:white_check_mark:](prime_factor/PrimeFactor.java) | [:white_check_mark:](prime_factor/prime_factor.py) | [:white_check_mark:](prime_factor/prime_factor.go) | [:white_check_mark:](prime_factor/primeFactor.js) | |
4344
| [Prims](https://en.wikipedia.org/wiki/Prim%27s_algorithm) | [:white_check_mark:](prims/prims.c) | | [:white_check_mark:](prims/Prims.java) | | [:white_check_mark:](prims/prims.go) | [:white_check_mark:](prims/prims.js) | |
@@ -85,7 +86,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
8586
* [Intro to Algorithms - Khan Academy](https://www.khanacademy.org/computing/computer-science/algorithms)
8687
* [Popular Data Structures and Algorithms - Codechef](https://discuss.codechef.com/questions/48877/data-structures-and-algorithms)
8788
* [Stanford-ACM-Codes](https://github.com/jaehyunp/stanfordacm) - A list of codes written by previous Stanford ACM team members and coaches.
88-
* [Data Structures and Algorithms](https://hackr.io/tutorials/learn-data-structures-algorithms) - A user ranked list of online tutorials to learn Data Structures and Algorithms online.
89+
* [Data Structures and Algorithms](https://hackr.io/tutorials/learn-data-structures-algorithms) - A user ranked list of online tutorials to learn Data Structures and Algorithms online.
8990

9091

9192

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class FibonacciNumber {
2+
3+
// fibonacci function return the nth fibonacci no for all n>0
4+
public static int fibonacci(int n) {
5+
if (n == 1 || n == 2) {
6+
return (n - 1);
7+
} else {
8+
// Store second last fibonacci number
9+
int a = 0;
10+
// Store last fibonacci number
11+
int b = 1;
12+
// Store current fibonacci number which is sum of last and second last fibonacci no
13+
int fib = 0;
14+
for (int i = 2; i < n; i++) {
15+
fib = a + b;
16+
a = b;
17+
b = fib;
18+
}
19+
return (fib);
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
// n>0
25+
int n = 5;
26+
if (n <= 0) {
27+
System.out.println("n must be greater than 0");
28+
return;
29+
}
30+
System.out.println(fibonacci(n));
31+
}
32+
}

0 commit comments

Comments
 (0)