Showing posts with label sum of digits in 100 factorial. Show all posts
Showing posts with label sum of digits in 100 factorial. Show all posts

Monday, February 6, 2012

sum of digits of 100!

/*
 * bhatt.umang7@gmail.com 
 */

/*
 * 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;

/**
 @author umang
 */
public class Problem_20
{

    public static void main(String args[])
    {
        BigInteger ans = new BigInteger("1");
        for (int i = 1; i <= 100; i++)
        {
            ans = ans.multiply(new BigInteger(new Integer(i).toString()));
        }

        long sum = 0;
        String s = ans.toString();
        for (int i = 0; i < s.length(); i++)
        {
            sum = sum + Long.parseLong("" + s.charAt(i));
        }
        System.out.print(sum);
    }
}
// ans is 648