Home >>Javascript Programs >JavaScript code to check number is Perfect or not
A number is said to be perfect when its value is equal to the sum of its proper positive divisors. The number is half the sum of all its positive or complete divisors (including itself) is known as perfect number.
In this tutorial we are going to check whether a number is perfect or not by using JavaScript.
Example: The first perfect number is 6, because 1, 2 and 3 are its positive divisors, and if we add 1+2+3 it is equal to 6. The number 6 is equal to half the sum of all its positive divisors.
Let's take an example:
<html> <head> <script> function completeNumber() { var flag,number,remainder,addition = 0,i; number = Number(document.getElementById("N").value); flag = number; for(i = 0; i < number; i++) { remainder = number%i; if(remainder==0) { addition += i; } } if(addition == flag) { window.alert(" number is Perfect"); } else { window.alert(" number is not Perfect"); } } </script> </head> <body> <br> <h1>check whether a number is Perfect or not</h1> Enter The Number :<input type="text" name="n" id = "N"/> <button onClick="completeNumber()">submit</button> </body> </html>
Enter The Number :