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

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -