Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
198
rated 0 times [  200] [ 2]  / answers: 1 / hits: 18660  / 7 Years ago, thu, august 31, 2017, 12:00:00

I need to do a code to verify whether or not the entered number is an Armstrong number, but my code does not work for every number.


Could anyone tell me what am I missing? Are there any other ways to do this?


Thank you!




let e, x, d = 0;
let b = prompt(Enter a number);
x=b;

while (x > 0) {
e = x % 10;
x = parseInt(x/10);
d = d + (e*e*e);
}

if (b==d)
alert(given number is an armstrong number);
else
alert(given number is not an armstrong number);

<!DOCTYPE HTML>
<html>
<head>
<title>Armstrong</title>
</head>
<body>
</body>
</html>




More From » javascript

 Answers
14

You can try for This, May this help for you:



 <!DOCTYPE HTML>
<html>
<head>
<title>Armstrong</title>
</head>
<body>

</body>
</html>

<script>
var z,e,x,d=0;
var b=prompt(Enter a number);
x=parseInt(b);
while(x>0) //Here is the mistake
{
e=x%10;
x=parseInt(x/10);
d=d+(e*e*e);
console.log(d: +d+ e: +e);

}
if(parseInt(b)==d){

alert(given no is amstrong number);
}
else {

alert(given no is not an amstrong number);
}
</script>

[#56614] Monday, August 28, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsley

Total Points: 352
Total Questions: 84
Total Answers: 94

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;