File tree Expand file tree Collapse file tree 2 files changed +14
-15
lines changed
main/java/com/thealgorithms/recursion
test/java/com/thealgorithms/recursion Expand file tree Collapse file tree 2 files changed +14
-15
lines changed Original file line number Diff line number Diff line change @@ -20,15 +20,15 @@ private Factorial() {
2020 * @return factorial of n
2121 * @throws IllegalArgumentException if n is negative
2222 */
23-
24- public static long factorial (int n ) {
23+
24+ public static long factorial (int n ) {
2525 if (n < 0 ) {
2626 throw new IllegalArgumentException ("Factorial is not defined for negative numbers." );
2727 }
28+
2829 if (n == 0 || n == 1 ) {
2930 return 1 ;
3031 }
3132 return n * factorial (n - 1 );
3233 }
3334}
34-
Original file line number Diff line number Diff line change 66import org .junit .jupiter .api .Test ;
77
88public class FactorialTest {
9- @ Test
10- public void testFactorialOfZero () {
11- assertEquals (1 , Factorial .factorial (0 ));
12- }
13-
14- @ Test
15- public void testFactorialOfOne () {
16- assertEquals (1 , Factorial .factorial (1 ));
9+ @ Test
10+ public void testFactorialOfZero () {
11+ assertEquals (1 , Factorial .factorial (0 ));
12+ }
13+
14+ @ Test
15+ public void testFactorialOfOne () {
16+ assertEquals (1 , Factorial .factorial (1 ));
1717 }
18-
18+
1919 @ Test
2020 public void testFactorialOfPositiveNumbers () {
2121 assertEquals (120 , Factorial .factorial (5 ));
2222 assertEquals (720 , Factorial .factorial (6 ));
2323 assertEquals (5040 , Factorial .factorial (7 ));
2424 }
25-
25+
2626 @ Test
2727 public void testFactorialOfTen () {
2828 assertEquals (3628800 , Factorial .factorial (10 ));
2929 }
30-
30+
3131 @ Test
3232 public void testNegativeNumberThrowsException () {
3333 assertThrows (IllegalArgumentException .class , () -> Factorial .factorial (-1 ));
3434 }
3535}
36-
You can’t perform that action at this time.
0 commit comments