C language practice question 19

foreword

This is the last post. I just wrote the topic, but didn't write the process and summary. Alas...
the holiday is over like this! too bad

Question 19

Topic:
A number is called a "perfect number" if it is exactly equal to the sum of its factors. For example, 6=1+2+3. Program to find all the perfect numbers within 1000.

My thinking:
This question is similar to the positive integer prime factor above~
1. Input: all numbers within 1000
Output: all perfect numbers within 1000
2. Analysis:

My process:

#include <stdio.h>
#define N 1000 //宏定义在很多时候会很方便
int main()
{
    
    
    int sum, x, i;
    for(x = 1; x <= N; x++)
	{
    
    
        sum = 0;
        for(i = 1; i <= x/2; i++)
           if(x%i == 0)
                sum += i;
           if(sum == x)
            printf("%d\n", x);
    }
    return 0;
}

Running results:
insert image description here
Summary:
Many times, I really want to create a flow chart, but I don’t know how to use the insert flow chart in this software, and I don’t have time to do it, so I give up.

Guess you like

Origin blog.csdn.net/weixin_52248428/article/details/116461234