I absolutely need to know which numbers from 1 to 1000 are considered perfect. From what I recall, a perfect number is a positive integer that is equal to the sum of all its divisors other than itself.
Example:
6 is a perfect number because 6 = 1 + 2 + 3
Output
A single line containing the perfect numbers separated by a space.
Note: The sample output below contains "dots" which are just representations (or dummies) for the actual values.
6·28·.·.·.
public class Main
{
public static boolean isPerfect(int n){
int sum=0;
for(int i=1;i<n;i++){
if(n%i==0){
sum+=i;
}
}
if(n==sum){
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
for (int i=1;i<=1000;i++){
if(isPerfect(i)==true){
System.out.print(i+ " ");
}
}
}
}
Comments
Leave a comment