Answer to Question #182285 in Java | JSP | JSF for salma wael

Question #182285

An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a method perfect that determines whether parameter number is a perfect number. Use this method in an application that determines and displays all the perfect numbers between 1 and 1000. Display the factors of each perfect number to confirm that the number is indeed perfect. Challenge the computing power of your computer by testing numbers much larger than 1000. Display the results


1
Expert's answer
2021-04-16T15:44:45-0400


public class PerfectNumber {


	public static void main(String[] args) {
		perfect();
	}

	public static void perfect() {
		int sumOfDigit = 0;
		for (int i = 0; i < 1001; i++) {
			sumOfDigit = 0;
			for (int j = 1; j < i; j++) {
				if (i % j == 0) {
					sumOfDigit = sumOfDigit + j;
				}
			}
			if (sumOfDigit == i && sumOfDigit != 0)
				System.out.print(i + "\t");
		}
	}
}

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