From 454bd8c0989d0e6b99163bba2d1fb8a1b87cb2eb Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 22:57:37 +0100 Subject: [PATCH] Create factorial calculation method --- src/main/java/Calculations.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/Calculations.java b/src/main/java/Calculations.java index 74654ff..09d1298 100644 --- a/src/main/java/Calculations.java +++ b/src/main/java/Calculations.java @@ -2,4 +2,18 @@ public class Calculations { public int calculateSquare(int 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; + } }