Perfect number: A number is called as perfect number when the sum of all it's factors divided by 2 gives the same number as a result. It's very simple, look at below expression
` (sum of all factors of n)/2 = n`
The first positive perfect number is 6. The factors of 6 are 1, 2, 3, and 6. The sum of all the factors of 6 is 12. So, 12/2 gives 6 again.
Algorithm
` BEGIN  DECLARE sum, i, number  INITIALIZE sum with 0 and i with 1  PRINT 'Enter a number'  ACCEPT number  LOOP UNTIL I reaches to number   IF number%i = 0   THEN    sum = sum + i   END IF  END LOOP  IF sum/2 = number  THEN   PRINT 'Perfect number'  ELSE   PRINT 'Not a perfect number'  END IF END`
Here is a Java program based on the above algorithm.
PerfectNumber.java
`import java.io.*;/** * This class demonstrates the perfect number. * @author SANTHOSH REDDY MANDADI * @version 1.0 * @since 27 August 2012 */class PerfectNumber{  public static void main(String args[]) throws Exception  {    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    System.out.println("Please enter a number:");    int number = Integer.parseInt(br.readLine());    if(isPerfectNumber(number))    {      System.out.println(number+" is a perfect number.");    }    else    {      System.out.println(number+" is not a perfect number.");    }  }  public static boolean isPerfectNumber(int number)  {    int sum = 0;    for(int i=1; i<=number; i++)    {      if(number%i == 0)      {        sum += i;      }    }    if(sum/2 == number)    {      return true;    }    return false;  }}`
Output
`>java PerfectNumberPlease enter a number:66 is a perfect number.>java PerfectNumberPlease enter a number:33 is not a perfect number.>java PerfectNumberPlease enter a number:81288128 is a perfect number.`
We can extend the above program to display the list of perfect numbers up to the given number.
PerfectNumbers.java
` import java.io.*;/** * This class prints all perfect numbers upto the given number. * @author SANTHOSH REDDY MANDADI * @version 1.0 * @since 27 August 2012 */class PerfectNumbers{  public static void main(String args[]) throws Exception  {    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    System.out.println("Please enter a number:");    int number = Integer.parseInt(br.readLine());    System.out.println("List of perfect numbers between 1 and "+number+": ");    int count = 0;    for(int i=2; i<=number;i++)    {      if(isPerfectNumber(i))      {        System.out.printf("%10d", i);        count++;      }    }    System.out.println("\n\nThere are "+count+" perfect numbers between 1 and "+number);  }  public static boolean isPerfectNumber(int number)  {    int sum = 0;    for(int i=1; i<=number; i++)    {      if(number%i == 0)      {        sum += i;      }    }    if(sum/2 == number)    {      return true;    }    return false;  }}`
Output
`>java PerfectNumbersPlease enter a number:10000List of perfect numbers between 1 and 10000:         6        28       496      8128There are 4 perfect numbers between 1 and 10000>java PerfectNumbersPlease enter a number:100000List of perfect numbers between 1 and 100000:         6        28       496      8128There are 4 perfect numbers between 1 and 100000`