mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Add input validation for negative values in FibonacciSeries (#7177)
This commit is contained in:
committed by
GitHub
parent
7d51c7f320
commit
48e02b3bac
@@ -12,10 +12,12 @@ public final class FibonacciSeries {
|
||||
throw new UnsupportedOperationException("Utility class");
|
||||
}
|
||||
public static int fibonacci(int n) {
|
||||
if (n < 0) {
|
||||
throw new IllegalArgumentException("n must be a non-negative integer");
|
||||
}
|
||||
if (n <= 1) {
|
||||
return n;
|
||||
} else {
|
||||
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||
}
|
||||
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user