JavaScript Program to Check Armstrong Number Example

Oct 25, 2021 . Admin



Hello Friends,

Now let's see example of how to check armstrong number example. We will check armstrong number using javascript. This is a short guide on armstrong number in javascript. Let's get started with how to check armstrong number in javascript.

Here i will give you many example how to check armstrong number using javascript.

Example : 1

<html>
    <head>
        <meta charset="utf-8">
        <title>JavaScript Program to Check Armstrong Number Example - MyWebtuts.com</title>
    </head>
    <body>
        <script type="text/javascript">
            let sum = 0;
            const num = prompt('Enter a three-digit positive integer: ');

            let temp = num;
            while (temp > 0) {
                let remainder = temp % 10;
                sum += remainder * remainder * remainder;
                temp = parseInt(temp / 10); 
            }
            if (sum == num) {
                document.write(`${num} is an Armstrong Number`);
            }
            else {
                document.write(`${num} is not an Armstrong Number.`);
            }
        </script>
    </body>
</html>
Output :
Enter a three-digit positive integer : 153
153 is an Armstrong Number
Example : 2
<html>
    <head>
        <title>JavaScript Program to Check Armstrong Number Example - MyWebtuts.com</title>
    </head>
    <body>
        <h1>Whether a number is Armstrong or not</h1>
        Enter The Number : <input type="text" id = "test"/>
        <br><br>
        <button onClick="Armstrong()">Check</button>
        
        <script>
            function Armstrong()
            {
                var flag,number,remainder,addition = 0;
                number = Number(document.getElementById("test").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>
    </body>
</html>
Output :
Enter The Number : 50
The inputed number is not Armstrong

It will help you...

#Javascript