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

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

零下一度
Release: 2017-06-25 10:24:53
Original
7539 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template