C # NÚMERO FELIZ
Autor: Wellington Fontes | Publicado: 2017-07-16T09:42:00-07:00

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
&nbsp; &nbsp; class Program
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)
&nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List&lt;int&gt; lista = new List&lt;int&gt;();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; 100; i++)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (VerificarNumero(i)) lista.Add(i);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var recebe = GerarListaNumeros();

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lista.ForEach(i =&gt; Console.WriteLine("Feliz {0}", i));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();

&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; 

&nbsp; &nbsp; &nbsp; &nbsp; public static List&lt;int&gt; GerarListaNumeros()
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List&lt;int&gt; lista = new List&lt;int&gt;();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; 100; i++)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (VerificarNumero(i)) lista.Add(i);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return lista;
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; public static bool VerificarNumero(int numero)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool numeroFeliz = false;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List&lt;int&gt; listaDigitos = new List&lt;int&gt;();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listaDigitos = DividirDigitos(numero);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; 20 &amp;&amp; !numeroFeliz; i++)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int sumaActual = CalcularQuadrados(listaDigitos);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sumaActual != 1)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listaDigitos = DividirDigitos(sumaActual);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else numeroFeliz = true;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return numeroFeliz;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; public static List&lt;int&gt; DividirDigitos(int digito)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List&lt;int&gt; digitos = new List&lt;int&gt;();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (digito != 0)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitos.Add(digito % 10);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digito = digito / 10;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return digitos;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; public static int CalcularQuadrados(List&lt;int&gt; listaDigitos)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int resultado = 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (int elem in listaDigitos) resultado += elem * elem;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return resultado;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
}