Home  >  Article  >  Java  >  Java classic programming question - find the sum of 1+2!+3!+...+20!

Java classic programming question - find the sum of 1+2!+3!+...+20!

零下一度
零下一度Original
2017-06-25 10:24:537391browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn