Calculate factorial of integers up to this value with long in Java
Description
The following code shows how to calculate factorial of integers up to this value with long.
Example
//from ww w . j a v a2s. c o m
public class Main {
public static void main(String[] args) {
long limit = 20;
long factorial = 1;
for (int i = 1; i <= limit; i++) {
factorial = 1; // Initialize factorial
int j = 2;
while (j <= i)
factorial *= j++;
System.out.println(i + "!" + " is " + factorial);
}
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »