+ 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
7 Respuestas
+ 7
I think a[i] (char) took assci value and not actual numeric value
+ 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
+ 4
But I have converted the int input into a string
+ 3
But what was the problem with my code?
+ 3
Oh thats why when i input 123 it outputs 150
+ 2
d = Char'.GetNumericValue(b[i]);
sum += (int)d;
Or
sum += Convert.ToInt32(Char.GetNumericValue(b[i]));