Skip to content

Commit ce14126

Browse files
committed
added factorial calculation snippet
1 parent 393ae33 commit ce14126

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

snippets/java/math/factorial.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Factorial
3+
description: Computes the factorial of a given number
4+
author: Mcbencrafter
5+
tags: math,number,factorial
6+
---
7+
8+
```java
9+
public static BigInteger factorial(int number) {
10+
BigInteger result = BigInteger.ONE;
11+
12+
for (int currentNumber = 1; currentNumber <= number; currentNumber++) {
13+
result = result.multiply(BigInteger.valueOf(currentNumber));
14+
}
15+
16+
return result;
17+
}
18+
19+
// Usage:
20+
int number = 6;
21+
System.out.println(factorial(number)); // 720 = 6*5*4*3*2
22+
```

0 commit comments

Comments
 (0)