void is_Perfectnumber(int number) //判断完数并输出
{
int i = 0;
int j = 0;
int sum = 0;
int arr[100] = { 0 };//定义一个数组,存储因子
for (i = 1; i <= number/2; i++) //判断完数
{
if (0 == number % i)
{
arr[j] = i;
j++;
sum = sum + i;
}
}
if (number == sum)
{
printf("%3d its factors are ",sum);
int k = 0;
for (k = 0; k < j; k++) //输出完数的各个因子
{
printf("%d ", arr[k]);
}
printf("\n");
}
}
int main()
{
int num = 0;
for (num = 1; num <= 1000; num++)
{
is_Perfectnumber(num);
}
system("pause");
return 0;
}
编程找出1000以内的完数,并按下面格式输出因子:6 its factors are 1 2 3
最新推荐文章于 2023-03-18 14:49:37 发布