Thursday, April 10, 2014

Project Euler Solution using C#: Problem 16: Power Digit Sum

Problem:

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^1000?
My Solution:

static void Main(string[] args)
{
    int pwr = 1000;
    int sum = 0;
    BigInteger l =(BigInteger)Math.Pow(2, 1000);
    foreach (var item in l.ToString().ToCharArray())
    {
        sum += Int32.Parse(item.ToString());
    }
    Console.WriteLine(sum);
    Console.ReadLine();
}

Note:
You can simplifies the coding :)

No comments:

Post a Comment