Conversion/String Problem
I was solving Project Eular-Problem 56 ( Problem 56 - Project Euler ), its relatively easy problem.But i encountered a small issue.I was so frustrated cause i couldn't find solution, so i have to google it, eventually i found the problem, and i need an explanation.
The problem was in sum of digits of digit:
Code:
for (int j = 0; j < 10000; j++)
{
s = Pdigits[j];
for (int i = 0; i < s.Length; i++)
array[j] += Convert.ToInt32(s[i]);
}
So here is my code (array Pdigits represent array of numbers), when i sum digits of eg. 99^99 i get 10440, and it should be 936.I just couldn't figure why.I found out that the problem was in
Code:
array[j] += Convert.ToInt32(s[i]);
(of course).
Here is the solution.
Code:
for (int j = 0; j < 10000; j++)
{
s = Pdigits[j];
char[] k = s.ToCharArray();
for (int i = 0; i < s.Length; i++)
array[j] += Convert.ToInt32(k[i].ToString());
}
With using char array k, and then converting it to string i get good output, my question is why?