A perfect number is a positive integer equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3.
Program:
import java.util.*;
class Rangeperfectnumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
int n = sc.nextInt();
for (int num = 1; num <= n; num++) {
int sum = 1;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0)
sum = sum + i;
}
if (sum == num) {
System.out.println(num +" is a Perfect number");
}
}
}
}
Output:
enter a number
100
1 is a perfect number
6 is a perfect number
28 is a perfect number
Note:- Find whether the given number is perfect or not - Link!
