current position:Home>C language function problem,.?!

C language function problem,.?!

2022-02-03 01:07:59 CSDN Q & A

This question requests to realize a function , Judge whether a natural number is perfect . If the sum of the factors of a natural number other than itself is equal to itself , Then the number is called the perfect number . for example 6 = 1 + 2 + 3; be 6 The number is perfect. .

Function interface definition :
The function interface is described here .:
int isPerfect (int x);
The interface parameters are explained here . among x Is the parameter passed in by the user . x The value of is not more than int Scope of and x Greater than 0; If x The number is perfect. , The function returns 1, Otherwise return to 0.

Sample referee test procedure :
Here is an example of a function being called for testing :

#include <stdio.h>

int isPerfect(int x);

int main()
{
for (int i = 1; i <= 10000; i++)
{
if (isPerfect(i))
{
printf("%d\n", i);
}
}
return 0;
}

/* Please fill in the answer here */




Refer to the answer 1:
#include <stdio.h>int isPerfect(int x);int main(){    for (int i = 1; i <= 10000; i++){        if (isPerfect(i)){            printf("%d\n", i);            }        }    return 0;}int isPerfect(int x){    int sum = 0;    for (int i=1; i<x; i++){        if (x%i==0)            sum += i;    }    if (x==sum)        return 1;    else        return 0;}

Output results :
6
28
496
8128




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030107575854.html

Random recommended