0

Cannot find a mistake

Hello, I want to ask you to help me. My code should sum all the digits of inputted integer. However program prints something wrong. for example: integer 12 digits sum is 3; integer 24 digit sum is 6, but program prints 50 and 52. Can you help me? Here is the code: Console.WriteLine("Input an integer: "); string number = Console.ReadLine(); int sum = 0; for (int i = 0; i < number.Length; i++) { sum =+ Convert.ToInt32(number[i]); } Console.WriteLine("Total sum of the digits is: " + sum);

5th Dec 2018, 9:42 AM
Tadas Janca
Tadas Janca - avatar
1 Resposta
0
Thanks for answer, Bennett Post. It works. I look for other function and found this: sum += (int)char.GetNumericValue(number[i]); It seems both of them works the same in this case.
5th Dec 2018, 10:53 AM
Tadas Janca
Tadas Janca - avatar