+ 3

What is wrong with this code?

I have made a code for calculating the sum of the digits of an number. It takes input in a string format. But the output is not the correct sum. Please help! https://code.sololearn.com/c2WCr12v18gm/?ref=app

29th Nov 2018, 4:32 AM
Kavish Bamboli
Kavish Bamboli - avatar
7 odpowiedzi
+ 7
I think a[i] (char) took assci value and not actual numeric value
29th Nov 2018, 6:22 AM
Rstar
Rstar - avatar
+ 7
Example You entered 123 String "123" Take 1 st character a[0] i.e '1' now your code will add 1 's ascii value i.e 49 to the sum and not 1
29th Nov 2018, 6:27 AM
Rstar
Rstar - avatar
+ 4
But I have converted the int input into a string
29th Nov 2018, 6:23 AM
Kavish Bamboli
Kavish Bamboli - avatar
+ 3
But what was the problem with my code?
29th Nov 2018, 6:20 AM
Kavish Bamboli
Kavish Bamboli - avatar
+ 3
Oh thats why when i input 123 it outputs 150
29th Nov 2018, 6:28 AM
Kavish Bamboli
Kavish Bamboli - avatar
+ 2
d = Char'.GetNumericValue(b[i]); sum += (int)d; Or sum += Convert.ToInt32(Char.GetNumericValue(b[i]));
30th Nov 2018, 10:37 AM
Евдакентий