2
Console.Write("Digite um numero de 4 digitos: ");
string numero = Convert.ToString(Console.ReadLine());
int soma = 0;
for(int i = 0; i < numero.Length; i++)
{
soma += Convert.ToInt32(numero[i]);
}
Console.WriteLine("Soma dos numeros = "+soma);
I know your put to convert the whole number (Convert.ToInt32(numero)
) converts normally, but when converting only the positions, the program converts the string
for HTML code, according to Unicode table, type, 2 = 50, 3 = 51
, all wrong. How do I convert each value of string
for int
?
I know, bro, you can do it in a lot of ways, but it’s not even about adding up the string values. The question that arose for me was the question of not being able to convert an index from a string to an integer. I want to know just that, how to convert, for example the string number = 2345, number[0] = 2, and take that number [0] and convert to integer, which I don’t know how, I’m looking up so far. if I convert using Convert.Toint32(number[0]), in this case it was supposed to give 2, but 50.
– paulo estevão
Actually the index itself is already separated and is an integer. The conversion is already happening in Lambda. The "digits" object is a list of integers. In your example 2345 the object is 2,3,4,5 (list of integers).
– rammonzito