Analyzing the code
There are some errors in your implementation. I made the corrections and I will comment below.
Console.Write("Informe um número inteiro para ser invertido: ");
string numeroDigitado = Console.ReadLine();
int numeroConvertido = Int32.Parse(numeroDigitado);
string invertido = "";
for (int i = numeroDigitado.Length; i > 0; i--) {
string letra = numeroDigitado.Substring(i - 1, 1);
invertido += letra;
}
Console.WriteLine(invertido);
First I changed the name of your variables to make it a little easier to read. This is not a rule.
Before you were getting a string from the Console, converting it to integer and then converting to string again, one of those conversions is unnecessary. You can see my changes on lines 2 and 3.
Your for
was starting at n
, which was your integer converted. The correct would be to vary by the number of characters of the number. 1235
must iterate 4 times, not 1235 times.
Another solution
An easy manipulation would be to take this integer and convert it to a vector of characters using the .ToCharArray()
method, invert this array and return to integer.
public int ReverseInt(int input) {
char[] vetor = input.ToString().ToCharArray();
Array.Reverse(vetor);
return Int32.Parse(new String(vetor));
}