Created
August 7, 2019 22:19
-
-
Save parzibyte/06389c153948c647a4e6b2897c37e375 to your computer and use it in GitHub Desktop.
Narcisista en C# - https://parzibyte.me/blog/2018/10/02/numero-narcisista-en-c-sharp/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Boolean EsNarcisista(int numero){ | |
string numeroComoCadena = numero.ToString(); | |
int longitudDelNumero = numeroComoCadena.Length; | |
double suma = 0; | |
// Recorrer toda la cadena carácter por carácter, y elevarlo a la potencia | |
// dada por la longitud del número | |
for(int indice = 0; indice < longitudDelNumero; indice++){ | |
char caracterActual = numeroComoCadena[indice]; | |
int cifraComoEntero = (int) Char.GetNumericValue(caracterActual); | |
double numeroElevado = Math.Pow(cifraComoEntero, longitudDelNumero); | |
suma = suma + numeroElevado; | |
} | |
if(suma == numero) return true; | |
else return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment