num[i]
is a char
and Convert.ToInt32
will return the ASCII code of the char instead of the actual numerical value.Use:
int number = Convert.ToInt32(num[i].ToString());
Also change i < num.Length-1
to i < num.Length
Edit: To make it more clear here is an example:
int n1 = Convert.ToInt32('0'); // Uses Convert.ToInt32(char) result -> 48
int n2 = (int) '0'; // cast from char to int result -> 48
int n3 = Convert.ToInt32("0"); // Uses Convert.ToInt32(string) result -> 0