Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (9.5k points)

Below is the code I’ve to check whether a number is an Armstrong number or not. The problem is my code doesn’t work for all numbers. Can anyone tell me what I’m doing wrong here?

var e, x, d = 0;

var 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>

1 Answer

0 votes
by (19.7k points)

Try the below code implementation:

<!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 armstrong number");

    }

    else {

    alert("given no is not an armstrong number");

    }

    </script>

Interested in Java? Check out this Java tutorial by Intellipaat.  

Related questions

0 votes
1 answer
asked Jan 6, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Apr 19, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...