código JavaScript a número de cheque es o no Armstrong

En este código JavaScript, vamos a comprobar si un número dado es el número Armstrong o no .

número Armstrong

Un número se dice que Armstrong cuando su valor es igual a su suma de cubo de dígitos.

Ejemplo:

    Input:
Number: 153
Output:
It is a prime number
Explanation:
153 = (1*1*1) + (5*5*5) + (3*3*3) = 1+125+27 = 153
Here, number (153) is equal to its sum of the digits cube (153).

código JavaScript para comprobar si un número es Armstrong o no

<script>
function Armstrong()
{
var flag,number,remainder,addition = 0;
number = Number(document.getElementById("N").value);
flag = number;
while(number > 0)
{
remainder = number%10;
addition = addition + remainder*remainder*remainder;
number = parseInt(number/10);
}
if(addition == flag)
{
window.alert("-The inputed number is Armstrong");
}
else
{
window.alert("-The inputed number is not Armstrong");
}
}
</script>

llamando a la función JavaScript en HTML (HTML, Código JS)

<html>
<head>
<script>
function Armstrong()
{
var flag,number,remainder,addition = 0;
number = Number(document.getElementById("N").value);
flag = number;
while(number > 0)
{
remainder = number%10;
addition = addition + remainder*remainder*remainder;
number = parseInt(number/10);
}
if(addition == flag)
{
window.alert("-The inputed number is Armstrong");
}
else
{
window.alert("-The inputed number is not Armstrong");
}
}
</script>
</head>
<body>
<br>
<h1>Whether a number is Armstrong or not</h1>
Enter The Number :<input type="text" name="n" id = "N"/>
<hr color="cyan">
<br>
<center><button onClick="Armstrong()">CHECK</button>
</body>
</html>

salida

JavaScript code to check number is Armstrong or not - 4


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *