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:

  1. static void Main(string[] args)
  2. {
  3. int pwr = 1000;
  4. int sum = 0;
  5. BigInteger l =(BigInteger)Math.Pow(2, 1000);
  6. foreach (var item in l.ToString().ToCharArray())
  7. {
  8. sum += Int32.Parse(item.ToString());
  9. }
  10. Console.WriteLine(sum);
  11. Console.ReadLine();
  12. }

Note:
You can simplifies the coding :)

No comments:

Post a Comment