计算并输出1000以内的所有“完数”之和

 

如果一个数恰好等于它的所有因子(包括1但不包括自身)之和,则称之为“完数”。例如6的因子为123,且1+2+3=6,因此6是一个“完数”。

    计算并输出1000以内的所有“完数”之和。

    具体要求如下:

    (1)所有循环均用for循环。

    (2)输出要有文字说明,并同时输出各“完数”。输出形式为

                完数1+完数2+=和值


#include<stdio.h>
#include
<conio.h>
#include
<math.h>
void main()
{
int sum=0,m,n,j,k;
printf(
"The sum of the over numbers that less than 1000 is:\n");
for(m=2;m<=1000;m++)
{
    k
=m/2,n=0;
    
for(j=1;j<=k;j++)
    
{
        
if(m%j==0)    n=n+j;
    }

    
if(m==n)
    
{
        printf(
"%d",m);printf("+");sum=sum+m;
    }

}

putchar(
'\b');
printf(
"=%d",sum);
getch();
}

 

posted @ 2007-01-20 15:49  齐心  Views(5615)  Comments(0Edit  收藏  举报