0
Write a program to print perfect numbers between 1 and 500.
Perfect numbers are numbers that are equal to the sum of its proper divisors. Ex:- 6 has proper divisors-> 1,2,3 And 6 = 1 + 2 + 3. Therefore, 6 is a perfect number.
2 odpowiedzi
+ 11
do you want us to write a program??
public class Program
{
public static void main(String[] args) {
System.out.println("perfect numbers between 1 and 500 are ");
for (int z = 2; z <= 500; z ++ ){
int prenum = perfectnumber(z);
if (prenum != 0){
System.out.println(prenum) ;
}
}
}
public static int perfectnumber(int x){
int y=0;
for (int count =1 ; count < x; count++){
if (x % count == 0) {
y += count ;
} // close if
} // close for
if (y == x) {
return y;
} else {
return 0;
}
}
}
+ 9
*Please* post your code to let us know what you have done so far and what your problem is in detail. *Thx*