**What is a perfect number?**

A positive integer whose sum of all factors(including 1 and excluding the number) is equal to the number itself is a **perfect number**.

Factors of a number are those numbers which completely divide the number. Complete division means that the remainder of division is zero.

**Example**

Consider 28. Factors of 28 are 1, 2, 4, 7, 14 and 28.

Sum of all factors excluding 28 is 1 + 2 + 4 + 7 + 14 = 28, hence it is a perfect number.

Refer **this** to find all factors of a number using java program.

**Algorithm**

Logic to determine if a given number is a perfect number consists of following steps.

1. Initialize a variable to hold the sum of factors to 0.

2. Loop from 1 till the number.

3. In every iteration, divide the number with current loop counter and check its remainder.

4. If the remainder is 0, means the current loop counter is a factor of number. Add the loop counter to the variable in step 1.

5. After the loop completes, compare the value stored in step 1 variable with the number. If both are equal, the number is a perfect number.

**Java program**

Java program based on the above algorithm is given below.

import java.util.Scanner; public class PerfectNumberChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number"); // read number from keyboard int number = scanner.nextInt(); scanner.close(); // variable to hold sum of factors int sum = 0; // iterate from 1 till one less than number for (int loopCounter = 1; loopCounter < number; loopCounter++) { // check if the loop counter is a factor of number if (number % loopCounter == 0) { // add it to sum sum = sum + loopCounter; } } // compare sum of factors and the number if (sum == number) { System.out.println(number + " is a perfect number."); } else { System.out.println(number + " is not a perfect number."); } } } |

Above program reads the number to be checked as **input from the user** using `java.util.Scanner`

class.

It then iterates from 1 till the one less than the number(since number needs to be excluded) and in every iteration checks if the loop counter completely divides the input number by comparing the remainder of division with 0.

To calculate remainder of division of two numbers, modulus operator(`%`

) is used. If the remainder is 0, then the loop counter is added to the sum of factors.

After the loop completes, the sum is compared with the input number to determine if it is a perfect number or not.

**Output**

Output of execution of the program is given below.

Enter a number

59

59 is not a perfect number.

Enter a number

28

28 is a perfect number.

**Using while loop**

Above program uses a `for`

loop for iteration but the same program can also be written using a `while`

loop as given below.

import java.util.Scanner; public class PerfectNumberChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number"); // read number from keyboard int number = scanner.nextInt(); scanner.close(); // variable to hold sum of factors int sum = 0; // initialize loop counter int counter = 1; // iterate from 1 till the number while(counter < number) { // check if the loop counter is a factor of number if (number % counter == 0) { // add it to sum sum = sum + counter; } counter++; } // compare sum of factors and the number if (sum == number) { System.out.println(number + " is a perfect number."); } else { System.out.println(number + " is not a perfect number."); } } } |

This method is exactly the same except that it uses a `while`

loop instead of `for`

.