【程序9】 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3。编程找出1000以内的所有完数。
public class Demo09 {
public static void main(String[] args) {
int s;
for (int i = 1; i <= 1000; i++) {
s = 0;
for (int j = 1; j < i; j++)
if (i % j == 0)
s = s + j;
if (s == i)
System.out.print(i + " " );
}
System.out.println();
}
}
或
public class Demo09{
public static void main(String[] args) {
int i,j,sum;
for(i=1;i<1000;i++)
{
sum = 0;
for(j=1;j<=i/2;j++)
{
if(i%j==0)
{
sum+=j;
}
}
if(sum==i)
{
System.out.print(i+" its factors are: ");
for(j=1;j<=i/2;j++)
{
if(i%j==0)
System.out.print(j+", ");
}
System.out.println();
}
}
}
}