JavaScript program to check perfect number

Write a JavaScript program to check whether a given number is perfect, abundant or deficient. Use an alert box to display the output.

<html>

<body>

    <h1>Perfect Number</h1>
    Enter a number:
    <input type="text" id="num" />
    <input type="button" value="Find" onClick="findPerfect()" />

    <script>
        function findPerfect() {
            var n = document.getElementById('num').value;
            var sum = 0;
            for (var i = 0; i < n; i++) {
                if (n % i == 0) {
                    sum = sum + i;
                }
            }
            if (sum == n)
                alert("The number is perfect ");
            else if (sum > n)
                alert("The number is abundant");
            else
                alert("The number is deficient");
        }
    </script>

</body>

</html>
Output
perfect js 1
perfect js 2
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments