Perfect Number Program in Java with Examples

In this tutorial, I will be sharing what is Perfect number, examples of Perfect number, algorithm, and java program to check whether a given number is Perfect number or not.

Read Also : Java Program to Check Duck Number

What is Perfect Number

Perfect number is a positive integer(number more than zero) that is equal to the sum of its proper positive divisors(or factors) excluding the number itself.
Examples :

Number to check : 28 
28 is divisible by 1,2,4,7,14 (Remember to exclude number itself i.e 28)
1 + 2 + 4 + 7 + 14 = 28 (sum of positive divisors is equal to given number 28)

                        // 28 is a Perfect number
 
Number to check : 6
6 is divisible by 1,2,3 (Remember to exclude number itself i.e 6)
1 + 2 + 3 = 6           (sum of positive divisors is equal to given number 6)
                        // 6 is a Perfect number
Number to check : 496 
496 is divisible by 1,2,4,8,16,31,62,124,248 (Remember to exclude number itself i.e 496)
1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496 (sum of positive divisors is equal to given number 496)
                        // 496 is a Perfect number
 

Below are examples of numbers which are NOT Perfect numbers

Number to check : 16 
16 is divisible by 1,2,4,8 (Remember to exclude number itself i.e 16)
1 + 2 + 4 + 8 = 15      (sum of positive divisors is NOT equal to given number 16)
                        // 16 is NOT a Perfect number
 
Number to check : 27 

27 is divisible by 1,3,9 (Remember to exclude number itself i.e 27)
1 + 3 + 9 = 13          (sum of positive divisors is NOT equal to given number 27)
                        // 27 is NOT a Perfect number



List of Perfect Numbers between 1 and 10000 are : 6, 28, 496, 8128

Java Program for Perfect Number using For loop


import java.util.Scanner;

public class JavaHungry {
    public static void main(String args[]) 
    {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter any number : ");
      int inputNumber = scan.nextInt();
      boolean result = isPerfectNumber(inputNumber);
      if (result == true)
        System.out.println(inputNumber + " is a Perfect Number");
      else 
        System.out.println(inputNumber + " is Not a Perfect Number");          
    }
    
    public static boolean isPerfectNumber(int num) 
    {
      /* Initialize sum variable indicating
 
         sum of divisors(factors) of inputNumber*/ 
      int sum = 0;
      for (int i=1;i < num;i++)
      {
          if ( num % i == 0)
          {
            sum = sum + i;
          }    
      }
      /* If sum variable is equal to inputNumber
         then the number is Perfect Number
         ,otherwise not */
      return (sum == num);
    }
}

Output :

Enter any number : 1000
1000 is Not a Perfect Number

Java Program for Perfect Number using While loop


import java.util.Scanner;

public class JavaHungry {
    public static void main(String args[]) 
    {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter any number : ");
      int inputNumber = scan.nextInt();
      boolean result = isPerfectNumber(inputNumber);
      if (result == true)
        System.out.println(inputNumber + " is a Perfect Number");
      else 
        System.out.println(inputNumber + " is Not a Perfect Number");          
    }
    
    public static boolean isPerfectNumber(int num) 
    {
      /* Initialize sum variable indicating
         sum of divisors(factors) of inputNumber*/
int sum = 0; int i=1; while(i < num) { if ( num % i == 0) { sum = sum + i; } i++; }
      /* If sum variable is equal to inputNumber
         then the number is Perfect Number
         ,otherwise not */
return (sum == num); } }

Output :

Enter any number : 1000
1000 is Not a Perfect Number

Java Program for finding Perfect Number between 1 to 1000

Below java program allows the user to enter range i.e minimum and maximum value. It will find the perfect number between the minimum and maximum values.


import java.util.Scanner;

public class JavaHungry {
    public static void main(String args[]) 
    {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter minimum number : ");
      int min = scan.nextInt();
      System.out.println("Enter maximum number : ");
      int max = scan.nextInt();
      isPerfectNumber(min,max);
    }
    public static void isPerfectNumber(int min, int max) 
    {
      int num = 0;
      for(num=min;num <= max;num++) 
      {
        int sum =0;   
        for (int i=1;i < num;i++)
        {
              if ( num % i == 0)
              {
                sum = sum + i;
              }    
        }
        if (sum == num)
        {
            System.out.println(num + " is a Perfect Number");
        }    
      }
    }
}

Output :
Enter minimum number : 1
Enter maximum number : 1000
6 is a Perfect Number
28 is a Perfect Number
496 is a Perfect Number

Algorithm for Perfect Number

1. Initialize the sum variable value to 0. It will represent the sum of divisors(factors) of a given inputNumber.

2. Using for/while loop
 a. Check if num(inputNumber) is divisible by i by using (num%i).
 b. If (num%i == 0), it indicates that i is a factor(divisor) of num(inputNumber). 
 c. Add the value of i to the sum variable.
 d. Continue till i is less than num(inputNumber).

3. Check whether the variable sum is equal to the num(inputNumber).
 a. If both are equal then the inputNumber is a Perfect number.
 b. Otherwise, the inputNumber is not a Perfect number.

Please mention in comments if you know any other way of implementing Perfect number program in java.

References:
Wikipedia

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry