20. Sum of the digits in the number 100!

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;
public class Problem20 {
public void sumofdigits() {
BigInteger num =  BigInteger.ONE;
for (int i=1;i<=100;i++){
num = num.multiply(BigInteger.valueOf(i));
}
final String bignumber = num.toString();
long result = 0;
for (int i = 0; i < bignumber.length(); i++) {
result += Integer.valueOf(String.valueOf(bignumber.charAt(i)));
}
System.out.println(“result: ” + result);
}

public static void main(String[] args) {
Problem20 p = new Problem20();
p.sumofdigits();
}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: