Write a Program to print All perfect numbers 1 to 10000


The program starts with a Perfect_Number() method that takes a number as an argument and checks whether it is a Perfect Number or not. It initializes sum as 0 and then iterates over all the possible divisors of the number up to num-1. For each divisor, it checks if it divides num without leaving a remainder. If it does, it adds it to the sum.

After the loop completes, the method checks whether the sum is equal to the num. If it is, the method returns true, indicating that the number is a Perfect Number. If it is not, the method returns false.

The program then runs a for loop from 1 to 10000. For each iteration, it calls the Perfect_Number() method to check if the current number is a Perfect Number or not. If it is, the program prints the number.

Source Code

class AllPerfect_Number  
{
        static boolean Perfect_Number(int num)
        {
            int sum = 0;
            for(int i=1; i<num; i++)
            {
                if(num%i==0)
                {
                    sum = sum+i;
                }
            }
            if(sum==num)
                return true;
            else
                return false;
        }
        public static void main(String args[])
        {
            for(int i=1; i<=10000; i++)
            {
                if(Perfect_Number(i))
                    System.out.println(i);
            }
        }
}

Output

6
28
496
8128

Example Programs