# Java Program to Check if a given Number is Perfect Number

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.

In this article, we will create java programs to check if a given number is perfect or not. for the given problem we are going to use iterative approaches like for loop and while loop. Let’s try to understand through some examples −

Example 1

Given number: 496

Its factors 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

Example 2

Given number: 54

Its factors are: 1, 2, 3, 6, 9, 18 and 27 ( we have to exclude 54 )

Sum of the factors are: 1 + 2 + 3 + 6 + 9 + 18 + 27 = 66

Therefore, it is not a perfect number

## Approach 1: Using For loop

### Syntax

for ( initial expression; conditional expression; increment/decrement expression )
{
// code to be executed
}


initial expression − executed once when loop begins.

conditional expression − code will be executed till conditional expression is true.

increment/decrement expression − to increment/decrement loop variable.

### Algorithm

• Step 1 − Declare and initialize an integer variable ‘n1’ to check if it is a perfect number or not and another integer variable ‘add’ to store the result of sum of factors of 496.

• Step 2 − Take a for loop that will run 495 times i.e. ‘n1-1’ because we have to exclude the given number. The if block inside for loop will check which number till 495 divides the number 496 completely and if it divides then it will increment ‘add’ variable with that number.

• Step 3 − The last if-else block will check whether the sum of factors is equal to given number or not. In the case of 496, the if block is true that’s why we will get result as 496 is a perfect number.

### Example

import java.util.*;
public class Perfect {
public static void main(String[] args) {
int n1 = 496;
for(int i = 1; i < n1; i++) {
if(n1 % i==0) {
}
}
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


## Approach 2: Using While loop

### Syntax

while (conditional expression) {
// code will be executed till conditional expression is true
increment/decrement expression;
// to increment or decrement loop variable
}


### Example

import java.util.*;
public class Main {
public static void main(String[] args) {
int n1 = 28;
int i = 1;
// loop variable
while(i < n1) {
if(n1 % i == 0) {
}
i++;
// 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 28 a perfect number?: true


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

## Approach 3: 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 becaue we can find all factors of a number (excluding number itself) in between half of that number.

### Example

import java.util.*;
public class Perfect {
public static void main(String[] args) {
int n1=6;
int i=1;
while(i <= n1/2) {
// loop will run till 3 ( 6/2 = 3)
if(n1 % i==0) {
}
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


## Conclusion

In this article, we have seen three approaches of java program to check if a given number is perfect or not. We understood how to use iterative approaches to make a java program. The approach 3 is more optimized and recommended by us.

Updated on: 25-Apr-2023

519 Views 