C language: Find the perfect number within the range of the input number.

#include<stdio.h>
int main()
{
	int n=0,i=0,sum;
	int number;
	scanf("%d",&number);
	for (n = 2; n <number; n++)
	{
		sum = 0;
		for (i = 1; i < n; i++)
		{
			if (n%i == 0)
			{
				sum += i;
			}
		}
		if (sum == n)
		{
			printf("Prefectnumber %d its factors are ", n);//factors表示因子
			for (i = 1; i < n; i++)
			{
				if (n%i == 0)
				{
				printf("%d ", i);
			}
			}
			printf("\n");
		}
	}
		return 0;
}

Use a for loop to get the factors of a number, find their sum, and then determine whether it is equal to the original number. If so, find its factors.


Guess you like

Origin blog.csdn.net/samxiaoguai/article/details/78587679