Answer to Question #269184 in Java | JSP | JSF for harryy

Question #269184

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·.·.·.




1
Expert's answer
2021-11-21T04:51:29-0500


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+ " ");
		    }
		}
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS