Check If a Given Number is Perfect Number in Java



When the sum of factors of a given number (after discarding the given number) is equal to the number itself is called a perfect number. The sum of these divisors is known as the aliquot sum. Therefore, we can say that a number is perfect if it matches its aliquot sum. All known perfect numbers are even.

Problem Statement

In this article, we will create Java programs to check if a given number is perfect or not. Let's try to understand the given problem through some examples:

Example Scenarios:

Input: number = 496
Output: 496 is a perfect number

Explanation:

Factors of 496 are: 1, 2, 4, 8, 16, 31, 62, 124, and 248 ( we have to exclude 496 )

Sum of the factors are: 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496

Therefore, it is a perfect number

Checking Perfect Number in Java

For the given problem, we are going to use the following approaches:

Check Perfect Number Using for Loop

The for loop is the entry control loop where the given condition is executed until. Following are the steps to check if a given number is a perfect number using a for loop -

  • Declare and initialize an integer variable n1 for the number to check and another integer variable add to store the sum of its factors.
  • Use a for loop that runs from 1 to n1 - 1.
  • Inside the loop, use an if statement to check if n1 is divisible by the current loop variable.
  • If divisible, add the loop variable to add.
  • After the loop, use an if-else block to check if add is equal to n1.

Example

A Java program that shows how to check if a given number is a perfect number is given below:

import java.util.*;
public class Perfect {
   public static void main(String[] args) {
      int n1 = 496;
      int add = 0;
      for(int i = 1; i < n1; i++) {
         if(n1 % i==0) {
            add = add + i;  
            // adding and incrementing 
         }
      }
      boolean isPerfect = (add == n1);
      if(isPerfect) {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      } else {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      }
   }    
}

Output:

is 496 a perfect number? : true 

Check Perfect Number Using while Loop

The while loop is an entry control loop, where the condition is checked before executing the loop's body. To check given number is perfect or not, use an if statement inside the while loop, which will check if the given number is divisible by the current loop variable. If divisible, add the current loop variable.

Example

In the following program, we have followed the same logic but with a different value of variable n1, and instead of a for loop, we have used a while loop.

import java.util.*;
public class Main {
   public static void main(String[] args) {
      int n1 = 28;
      int add = 0;
      int i = 1;  
      // loop variable
      while(i < n1) {
         if(n1 % i == 0) {
            add = add + i; 
         }
		 // incrementing 
         i++; 
      }
      boolean isPerfect = (add == n1);
      if(isPerfect) {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      } else {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      }
   }    
}

Output:

is 28 a perfect number? : true

Check Perfect Number by Running the Loop Till n/2

This approach is more optimized than the other two approaches we have discussed earlier in this article. In this approach, the loop will iterate till half of the given number only because we can find all factors of a number (excluding the number itself) in between half of that number.

Example

Let's implement the above approach in a Java program:

import java.util.*;
public class Perfect {
   public static void main(String[] args) {
      int n1=6;
      int add = 0;
      int i=1;
      while(i <= n1/2) { 
         // loop will run till 3 ( 6/2 = 3)
         if(n1 % i==0) {
            add = add + i;
         }
         i++;
      }
      boolean isPerfect = (add == n1);
      if(isPerfect) {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      } else {
         System.out.println("is " + n1 + " a perfect number?: " + isPerfect);
      }
   }    
}

Output:

is 6 a perfect number?: true
Updated on: 2025-06-06T13:17:34+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements