Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions Maths/Factorial.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ public static void main(String[] args) {
* @return the factorial of {@code n}
*/
public static long factorial(int n) {
if (n < 0) {
throw new ArithmeticException("n < 0"); //Dont work with less than 0
}
long fac = 1;
for (int i = 1; i <= n; ++i) {
fac *= i;
}
return fac; //Return factorial
// Using recursion
try {
if (n == 0) {
return 1; // if n = 0, return factorial of n;
}else {
return n*factorial(n-1); // While N is greater than 0, call the function again, passing n-1 (Principle of factoring);
}
}catch (ArithmeticException e) {
System.out.println("Dont work with less than 0");
}
return n;
}
}