recursion - Handling negative integers in recursive factorial function in Java -
i have written recursive factorial function in java. program works fine, want function show message if integer input given negative , quit immediately rather calling recursively.
how possible?
the factorial operation defined non-negative integers. java way handle throw illegalargumentexception
on negative input:
public static int factorial (int n) { if (n < 0) { throw new illegalargumentexception ("n must non-negative"); } if (n == 0) { return 1; } return n * factorial (n - 1); }
Comments
Post a Comment