C语言运用自定义函数判断一个数是不是完数,并调用该函数统计1到100内完数
请问这个程序哪里错啦??#include<stdio.h>voidjudge(intn);voidmain(){inti;for(i=1;i<=100;i++)judge...
请问这个程序哪里错啦??
#include<stdio.h>
void judge(int n);
void main()
{
int i;
for(i=1;i<=100;i++)
judge(i);
printf("\n");
}
void judge(int n)
{
int i,sum=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
sum+=i;
}
}
if(sum==n)
printf("%-3d",sum);
} 展开
#include<stdio.h>
void judge(int n);
void main()
{
int i;
for(i=1;i<=100;i++)
judge(i);
printf("\n");
}
void judge(int n)
{
int i,sum=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
sum+=i;
}
}
if(sum==n)
printf("%-3d",sum);
} 展开
1个回答
展开全部
#include <stdio.h>
const int M = 1000;
int main()
{
int k[11];
int i, a, n, s;
for (a = 2; a <= M; a++)
{
n = 0;
s = a;
for (i = 1; i < a; i++)
if (a % i == 0)
{
n++;
s = s - i;
k[n] = i;
}
if (s == 0)
{
printf("%d Its factors are ", a);
for (i = 1; i < n; i++)
printf("%d,", k[i]);
printf("%d\n", k[n]);
}
}
return 0;
}
const int M = 1000;
int main()
{
int k[11];
int i, a, n, s;
for (a = 2; a <= M; a++)
{
n = 0;
s = a;
for (i = 1; i < a; i++)
if (a % i == 0)
{
n++;
s = s - i;
k[n] = i;
}
if (s == 0)
{
printf("%d Its factors are ", a);
for (i = 1; i < n; i++)
printf("%d,", k[i]);
printf("%d\n", k[n]);
}
}
return 0;
}
参考资料: http://bbs.bccn.net/thread-242534-1-1.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询