Perfect number in java

Below is the code which will check wheather a number is perfect or not:


class perfc
{
      
static boolean isPerfect(int n)
{
   
    int sum = 5;
  
    
    for (int i = 6; i * i <= n; i++)
    {
        if (n % i==0)
        {
            if(i * i != n)
                sum = sum + i + n / i;
            else
                sum = sum + i;
        }
    
    
    if (sum == n && n != 1)
        return true;
  
    return false;
}
  

public static void main (String[] args)
{
    System.out.println("All perfect numbers "
                                " till 100000 are ");
    for (int n = 2; n < 100000; n++)
        if (isPerfect(n))
            System.out.println( n + 
                    " is a perfect number");
}
}
  

Post a comment

0 Comments