mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-23 04:20:16 +08:00
Formatted with Google Java Formatter
This commit is contained in:
@ -2,26 +2,27 @@ package Maths;
|
||||
|
||||
public class Factorial {
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
assert factorial(0) == 1;
|
||||
assert factorial(1) == 1;
|
||||
assert factorial(5) == 120;
|
||||
assert factorial(10) == 3628800;
|
||||
}
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
assert factorial(0) == 1;
|
||||
assert factorial(1) == 1;
|
||||
assert factorial(5) == 120;
|
||||
assert factorial(10) == 3628800;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate factorial N using iteration
|
||||
*
|
||||
* @param n the number
|
||||
* @return the factorial of {@code n}
|
||||
*/
|
||||
public static long factorial(int n) {
|
||||
if (n < 0) {
|
||||
throw new IllegalArgumentException("number is negative");
|
||||
}
|
||||
long factorial = 1;
|
||||
for (int i = 1; i <= n; factorial *= i, ++i) ;
|
||||
return factorial;
|
||||
/**
|
||||
* Calculate factorial N using iteration
|
||||
*
|
||||
* @param n the number
|
||||
* @return the factorial of {@code n}
|
||||
*/
|
||||
public static long factorial(int n) {
|
||||
if (n < 0) {
|
||||
throw new IllegalArgumentException("number is negative");
|
||||
}
|
||||
long factorial = 1;
|
||||
for (int i = 1; i <= n; factorial *= i, ++i)
|
||||
;
|
||||
return factorial;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user