Browse Source

Create factorial calculation method

remotes/origin/test/client/add-test-cases
Alena Bandarovich 11 months ago
parent
commit
454bd8c098
  1. 14
      src/main/java/Calculations.java

14
src/main/java/Calculations.java

@ -2,4 +2,18 @@ public class Calculations {
public int calculateSquare(int num) { public int calculateSquare(int num) {
return (num * num); return (num * num);
} }
public int calculateFactorial(int num) {
if (num < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative numbers");
}
if (num == 0 || num == 1) {
return 1; // Factorial of 0 and 1 is 1
}
int factorial = 1;
for (int i = 2; i <= num; i++) {
factorial *= i;
}
return factorial;
}
} }
Loading…
Cancel
Save