c语言求解完数!

2024-12-20 19:56:02
推荐回答(2个)
回答1:

#include
int main()
{
int i,j,sum=0;
int count=0;//计算完数的和
for(i=2;i<1000;i++)
{
for(j=1;j {
if(i%j==0) sum=sum+j;
}
if(sum==i)
{
printf("%d 是完数\n",i);
count+=i;
}
sum=0;
}

printf("完数和为: %d\n",count);
return 0;
}

回答2:

#include
#include
#define N 1000
void main()
{
int num,f[20],i,j,sum,t;
unsigned short flag;
/*Out Recycle*/
for(num = 2;num {
sum=0;
j= 0;

/*Inner Recycle*/
for(i=1;i<=num/2;i++)
{
if(num%i==0)
{
f[j]=i;
sum+=f[j];
j++;
}
}

/*justify it is a perfect num*/
t=j-1;
if(sum==num)
{
printf("%d its factors are",num);
for(j=0;j<=t;j++)
printf("%d,",f[j]);
printf("\n");
}

/* enter next column*/
}
system("pause");
}