# Perfect number

A number is called perfect if the number is equal to the sum of its divisors (the number itself is not considered a divisor in this case).

## Check Perfect Number

The above code checks if a given number is a perfect number or not. A perfect number is a positive integer that is equal to the sum of its proper positive divisors, excluding the number itself.

It first takes the number as input using the Scanner class. It then initializes the sum variable with 0.

It then uses a for loop to find the sum of all positive divisors of the given number. The loop starts from 1 and continues till the number, and for each iteration, it checks if the number is divisible by i (the current iteration value). If the number is divisible, then the value of i is added to the sum.

After the loop ends, the code checks if the sum is equal to the given number. If yes, then the number is considered as a perfect number, and a message is displayed saying so. If the sum is not equal to the number, then the number is not a perfect number, and a message is displayed accordingly.

``````import java.util.Scanner;

public class PerfectNumber {
public static void main(String[] args) {
int number, sum = 0;

System.out.println("Enter a number to check for perfect: ");

for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum = sum + i;
}
}

if (sum == number) {
System.out.print(number + " is a perfect number");
} else {
System.out.print(number + " is not a perfect number");
}
}
}``````
##### Output
``````Enter a number to check for perfect: 28
28 is a perfect number``````

## Perfect Number List (1-n)

This Java program calculates and displays the perfect numbers within a given range (up to an upper bound `max`). The program defines a function `checkPerfect` which takes the maximum limit as input and calculates the perfect numbers within the given range. The program first defines an integer variable `number` and sets it equal to the minimum value `min` (which is set to 1). Then, using a for loop, the program iterates `number` from `min` to `max`. For each iteration, the program initializes `sum` to 0 and `i` to 1. Then, using a nested for loop, the program iterates `i` from 1 to `number`-1, and adds `i` to `sum` if `number` is divisible by `i`. Finally, if `sum` equals `number`, the program prints `number` and the message “is a perfect number”.

The main function reads the upper bound value from the user, calls the `checkPerfect` function with the upper bound as an argument, and then closes the `Scanner` object.

``````import java.util.Scanner;

public class PerfectNumberSeries {
private static void checkPerfect(int max) {
int number, min = 1, i, sum = 0;

for (number = min; number <= max; number++) {
sum = 0;
for (i = 1; i < number; i++) {
if (number % i == 0) {
sum = sum + i;
}
}
if (sum == number) {
System.out.println(number + " is a perfect number");
}
}
}

public static void main(String[] args) {
int max;

System.out.print("Enter the upper bound to find perfect numbers: ");
``````Enter the upper bound to find perfect numbers: 10000