forked from sahua3228/Euler-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem20.java
30 lines (25 loc) · 834 Bytes
/
Problem20.java
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
29
30
/*n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!*/
import java.math.BigInteger;
import java.util.Scanner;
public class Problem20 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
BigInteger fact= BigInteger.valueOf(1);
for(long i=1; i<=100; i++)
{
fact=fact.multiply(BigInteger.valueOf(i));
}
BigInteger sum= BigInteger.valueOf(0);
BigInteger zero=BigInteger.valueOf(0);
while(!fact.equals(zero))
{
sum=sum.add(fact.remainder(BigInteger.valueOf(10)));
fact=fact.divide(BigInteger.valueOf(10));
}
System.out.println("Sum of 100!="+sum);
}
}
//Output=648