Reducir un número en C#

using System; using System.Collections.Generic; namespace curso { class Principal { static void Main(string[] args) { inicio() } static int sumarNumero(int numero) { int suma = 0; string str = Convert.ToString(numero); for (int i = 0; i < str.Length; i++) { char car = str[i]; string tmp = Convert.ToString(car); suma += Convert.ToInt32(tmp); } if(Convert.ToString(suma).Length >= 2) { suma = sumarNumero(suma); } return suma; } static void inicio() { List<int> numeros = new List<int>(){520,544,563,602,621,662,704,721,733,789,804,832,845,911,939,1200,1250,1266,1350,1363,1490,1500,1555,1633,1666}; foreach(var numero in numeros) { Console.WriteLine("{0} : {1} ",numero,sumarNumero(numero)); } } } }
De una lista de números enteros reducir cada elemento hasta lo máximo.

Si la entrada es 402 debe salir 6
Si la entrada es 121 debe salir 4

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.