Add input validation for negative values in FibonacciSeries (#7177)

This commit is contained in:
Adityaraj Solanki
2025-12-18 21:39:55 +05:30
committed by GitHub
parent 7d51c7f320
commit 48e02b3bac

View File

@@ -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);
}
}