Find the sum of 1+2!+3!+...+20!.
public class Example21 {
public static void main(String[] args) {
sum(20);
}
public static void sum(int n) {
long sum = 0;
long fac = 1;
for (int i = 1; i <= n; i++) {
fac *= i;
sum += fac;
}
System.out.println("1! to " + n + "! The sum added is: " + sum);
}
}
The above is the detailed content of Java classic programming question - find the sum of 1+2!+3!+...+20!. For more information, please follow other related articles on the PHP Chinese website!