(perfect Number) An integer number said to be a perfect if its factors, includin
ID: 3624355 • Letter: #
Question
(perfect Number) An integer number said to be a perfect if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6= 1+ 2+3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 to 1000. print the factors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000.You MUST use a function to determine the perfect number. Also write a function that determines the factors of a number. You must use pointers for these functions (though they can be written without them) for the practice.
Functions are defined above main() (-10 pts)
Functions don't use pointers (-10)
Global variables are used (-20)
Functions are not used (-30)
Program appears to be shared or another person's work (no credit) for all
Explanation / Answer
include <stdio.h>
static int perfect ( int n )
{
int i ;
int sum = 0 ;
for ( i = 1 ; i <= n / 2 ; ++i ) if ( n % i == 0 ) sum += i ;
return sum == n ;
}
int main ( )
{
int i ;
for ( i = 1 ; i <= 1000 ; ++i )
{
if ( perfect ( i ) ) printf ( "%d " , i ) ;
}
return 0 ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.