Write an application that displays every perfect number from 1 through 1,000. A
ID: 3767627 • Letter: W
Question
Write an application that displays every perfect number from 1 through 1,000. A
perfect number is one that equals the sum of all the numbers that divide evenly into it.
For example, 6 is perfect because 1, 2, and 3 divide evenly into it, and their sum is 6;
however, 12 is not a perfect number because 1, 2, 3, 4, and 6 divide evenly into it, and
their sum is greater than 12. Save the file as Perfect.java.
Again, I would like to thank you guys for all your help and for the explanation that follows.
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Perfect
{
public static boolean isPerfectNumber(int number){
int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
if(temp == number)
return true;
else
return false;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
for(int i=1;i<=1000;i++)
{
if(isPerfectNumber(i)==true)
System.out.println(i+" is a perfect number.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.