KaiquanMah's picture
Create 7. Factorial
367fcb1 verified
raw
history blame
983 Bytes
Example function calls:
public static void main(String[] args) {
System.out.println(factorial(3));
int f = factorial(4);
System.out.println(f);
}
Program outputs:
6
24
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
int[] p = {2,3,4,6,1};
for (int pa : p) {
System.out.println("Testing with parameter value " + pa);
System.out.println("Factorial: " + factorial(pa));
System.out.println("");
}
}
public static int factorial(int pa) {
int fact = 1;
for (int i=1; i<=pa; i++) {
fact *= i;
}
return fact;
}
}
Testing with parameter value 2
Factorial: 2
Testing with parameter value 3
Factorial: 6
Testing with parameter value 4
Factorial: 24
Testing with parameter value 6
Factorial: 720
Testing with parameter value 1
Factorial: 1