Skip to content

Commit 48e02b3

Browse files
authored
Add input validation for negative values in FibonacciSeries (#7177)
1 parent 7d51c7f commit 48e02b3

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/main/java/com/thealgorithms/recursion/FibonacciSeries.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ private FibonacciSeries() {
1212
throw new UnsupportedOperationException("Utility class");
1313
}
1414
public static int fibonacci(int n) {
15+
if (n < 0) {
16+
throw new IllegalArgumentException("n must be a non-negative integer");
17+
}
1518
if (n <= 1) {
1619
return n;
17-
} else {
18-
return fibonacci(n - 1) + fibonacci(n - 2);
1920
}
21+
return fibonacci(n - 1) + fibonacci(n - 2);
2022
}
2123
}

0 commit comments

Comments
 (0)