Select Page

## What are Perfect Numbers?

Perfect Numbers are those numbers whose sum of factors (excluding the number itself) equals to the number itself.

### Lets understand by example:

Lets take number 9:

The factors of 9 are : 1, 3, 9

excluding 9 itself we are left with 1 and 3, so the sum of 1 and 3 is equal to 4 and is not equal to the number 9 itself, hence we can say 9 is not a perfect number.

Lets take another example:

Lets take the number 6:

The factors of 6 are : 1,2,3,6

excluding the number 6 itself we are left with 1, 2 and 3, so the sum of 1, 2 and 3 is equal to 7, which is again equal to the number 6 itself, hence we can say number 6 is a perfect number.

Now lets find the programmatic solution for finding Perfect Number using pure java, code provided below:

```import java.util.Scanner;
class Perfect_Number
{
public int chk_perfectNumber(int n)
{
int sum=0;
for(int i = 1; i < n; i++)
{
if(n % i == 0)
{
sum = sum + i;
}
}
if(sum == n)
{
return 1;
}
else
{
return 0;
}
}
}

public class PerfectNumber
{
public static void main(String[] args)
{
int n, chk = 0;
Scanner s = new Scanner(System.in);
Perfect_Number PN = new Perfect_Number();
System.out.print("Enter a number to find it is Perfect Number or not:");
n = s.nextInt();
chk=PN.chk_perfectNumber(n);

if(chk == 1)
{
System.out.println("Given number is Perfect");
}
else
{
System.out.println("Given number is not Perfect");
}
}
}```

Compile the above program using command line compiler, and you can run it using java interpreter commands, steps given below:

First Compile:

• javac PerfectNumber.java
• It will create a PerfectNumber.class

Now you can run this program:

• java PerfectNumber

This is a common question being put forward during exam, especially for CBSE Class XI (having Computer Science with C++ or Python, or Informatics Practices (IP) Students also. The code above is written in pure Java, class XIth students must follow the links below to see the code specific to their stream or language for Finding Perfect Number.

Other useful links:

Pawan Arora AdministratorKeymaster
Founder , Edukers
Teaching, Coding and Sharing is his passion. A true mentor and motivator. C/C++, Python, Java, Web Technologies (html5 / CSS/ Javascript/ JQuery,Bootstrap, nodeJS ,PHP etc.) and a WordPress enthusiast with more than two decades of experience.
follow me