blob: f81c54a5b1e36bd1c2599b252d7213f8525fffc2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/*
* Run this as:
* java Factorial
*/
public class Factorial {
public static void main(String[] args) {
int x = factorial(5);
System.out.println(x);
int y = factorial(10);
System.out.println(y);
System.out.println(factorial(3));
}
/*
* Here is a subroutine defined. In Java, we call it a method.
*/
public static int factorial(int x) {
if (x == 0) {
return 1;
}
return x * factorial(x - 1);
}
}
|