Output all finished numbers of 1-1000

The rule of the complete number: the sum of all its true factors (ie the divisors other than itself) is exactly equal to itself.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void main()
{
	int num,n,i;
	int fct,sum;
	int a[50];
	
	for(num=2;num<=1000;num++)
	{
		n=num/2;
		sum=0;
		i=0;
		for(fct=1;fct<=n;fct++)
		{
			if(num%fct==0)//注意括号 
			{
			 sum += fct;
			 a[i++]=fct; //i记得自增 
			}
		}
		n=i;
		if(sum == num)
		{
		  printf("%d是完数,因子为:",num);
		  for(i=0;i<n;i++)
		  printf(" %d ",a[i]); 
		  printf("\n");//换行不要加错位置 
		}
	
	}
}
Published 11 original articles · Likes0 · Visits1

Guess you like

Origin blog.csdn.net/qq_38272075/article/details/105527988