Turn Desktop View Off
by Dinesh Thakur

A Perfect Number is sum of its positive divisors, excluding the number itself. The First Perfect number is 6, For example 6 is a perfect number as 1,2 and3 are its divisors and the sum of divisors= ( 1 + 2 + 3)  = 6.

Algorithm for Perfect Number Program:

 

step 1: Read num

step 2: Set b=1, s=0

step 3: Repeat through step-5 until (b < num)

step 4: If (num mod b) equals to 0 then s=s+b

step 5: b=b+1

step 6: If s equals to num then print “num is perfect”

Else print “num is not perfect”

Step 7: Exit

 

Here is the Java Example for Perfect Number Program:

import java.util.Scanner;

class PerfectNumber
{
    public static void main(String args[])
    {
        Scanner sl=new Scanner(System.in);
        System.out.println("Enter A Number");
        int num=sl.nextInt();
      
        int b=1, s=0;
      
        while(b<num )
        {
            if(num%b == 0)
            {
                s += b;
             
            }
            b++;
        }
      
        if(s == num)
        {
            System.out.println(num+" is a perfect number");
        }
       else
        {
           System.out.println(num+" is not a perfect number");
        }      
    }
}

Perfect Number Program in Java