C Program: An integer number is said to be a perfect number 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.  I have written a function called isPerfect (see below), that determines whether parameter passed to the function is a perfect number.  Use this function in a C program that determines and prints all the perfect numbers between 1 and 1000.  Print the factors of each perfect number to confirm that the number is indeed perfect.   // isPerfect returns true if value is perfect integer, // i.e., if value is equal to sum of its factors int isPerfect(int value) {    int factorSum = 1; // current sum of factors      // loop through possible factor values    for (int i = 2; i <= value / 2; ++i) {         // if i is factor       if (value % i == 0) {          factorSum += i; // add to sum       }       }       // return true if value is equal to sum of factors    if (factorSum == value) {       return 1;    }     else {       return 0;    }  }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter6: User-defined Functions
Section: Chapter Questions
Problem 17PE
icon
Related questions
Question

C Program:

An integer number is said to be a perfect number 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.  I have written a function called isPerfect (see below), that determines whether parameter passed to the function is a perfect number.  Use this function in a C program that determines and prints all the perfect numbers between 1 and 1000.  Print the factors of each perfect number to confirm that the number is indeed perfect.

 

// isPerfect returns true if value is perfect integer,

// i.e., if value is equal to sum of its factors

int isPerfect(int value)

{

   int factorSum = 1; // current sum of factors

 

   // loop through possible factor values

   for (int i = 2; i <= value / 2; ++i) {

 

      // if i is factor

      if (value % i == 0) {

         factorSum += i; // add to sum

      } 

 

   } 

 

   // return true if value is equal to sum of factors

   if (factorSum == value) {

      return 1;

   } 

   else {

      return 0;

   } 

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Concept of Parenthesis
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr