JavaScript Program to Check Perfect Number

You are Here:

What is Perfect Number?

A positive integer that is equal to the sum of its proper divisors.

For example, 6 is a perfect number
6 = 1 + 2 + 3
6 = 6

Tips: It is recommended to use our online Perfect Number calculator for better understanding.

Check Perfect Number

In the following example, we will check whether the given number (496) is a Perfect number or not.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <h1>JS Perfect Number</h1> <script> var num = 496; var i = 1; var total = 0; for(i = 1; i < num; i++) { if(num % i == 0) total += i; } if(total == num) document.write(num +" is a perfect number"); else document.write(num +" is not a perfect number"); </script> </body> </html>

Perfect Numbers between the Given Range

In the following example, we will find all the Perfect numbers between 1 and 1000.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <h1>JS Find All Perfect Numbers</h1> <script> var start = 1; var end = 1000; var i = 1; var total = 0; var flag = 0; for(start = start; start <= end; start++) { for(i = 1; i < start; i++) { if(start % i == 0) total += i; } if((total == start) && (start != 0)) { if(flag < 1) { document.write("Perfect numbers are: "); flag = 1; } document.write(start +", "); } total = 0; } if(flag == 0) document.write("There in no perfect number"); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author