Question

In: Computer Science

In this question, you should complete the C program, perfect.c, by implementing the function isPerfect. Open...

In this question, you should complete the C program, perfect.c, by implementing the function isPerfect. Open the file perfect.c, complete, compile and run it. When you are sure it is correct, include the c file in your final submission.

* Note: You should first carefuly read the COMMENTS provided for the function isPerfect, and then start completing the function.

* Note: You MUST NOT alter the main function and the prototype/header of the isPerfect function. ONLY develop the body of the function isPerfect.

A perfect number:
An integer number is said to be a perfect number if its factors, including 1 (but not the

number itself), sum to the number.
Examples:
6 is a perfect number because its factors, except itself, are 3, 2, and 1, and 6 = 3+2+1.

12 is not a perfect number because its factors, except itself, are 6, 4, 3, 2, and 1, but 12 ≠ 6+4+3+2+1.

The code:

#include <stdio.h>

/*
* 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 its factors, except itself, are 3, 2, and 1, and 6 = 3+2+1, but 12 is not a perfect
* number because its factors, except itself, are 6, 4, 3, 2, and 1, but 12 ≠ 6+4+3+2+1.
* This function determines whether its integer parameter, num, is a perfect number or not by returning 1 if the number is a perfect number,
* and 0 otherwise.
*/
int isPerfect(int num);

int main(void) {

   for (int i=1; i<=1000;i++) {

       if (isPerfect(i))

           printf("%d is perfect.\n", i);
   }

}

int isPerfect(int num) {

   // Complete the code of the function

}

Solutions

Expert Solution

Screenshot of program code:-

Screenshot of output:-

Program code to copy:-

#include <stdio.h>

/*
* 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 its factors, except itself, are 3, 2, and 1, and 6 = 3+2+1, but 12 is not a perfect
* number because its factors, except itself, are 6, 4, 3, 2, and 1, but 12 ? 6+4+3+2+1.
* This function determines whether its integer parameter, num, is a perfect number or not by returning 1 if the number is a perfect number,
* and 0 otherwise.
*/
int isPerfect(int num);

int main(void) {

for (int i=1; i<=1000;i++) {
if (isPerfect(i))
printf("%d is perfect.\n", i);
}
}

int isPerfect(int num) {
    int sum = 0;
    for(int i=1; i<num; i++) {
       if(num%i == 0)
           sum += i;
    }
if(sum == num)
        return 1;
    else
        return 0;
}


Related Solutions

Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Write a python program for the following question. Complete the symptom similarity function, which measures the...
Write a python program for the following question. Complete the symptom similarity function, which measures the similarity between the symptoms of two patients. See below for an explanation of how the similarity is computed. def symptom_similarity(symptoms_A: Tuple[Set], symptoms_B: Tuple[Set]) -> int: '''Returns the similarity between symptoms_A and symptoms_B. symptoms_A and symptoms_B are tuples of a set of symptoms present and a set of symptoms absent. The similarity measure is computed by the following equations: present_present + absent_absent - present_absent -...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
C++, Complete this program as directed // This program will read in a group of test...
C++, Complete this program as directed // This program will read in a group of test scores (positive integers from 1 to 100) // from the keyboard and then calculate and output the average score // as well as the highest and lowest score. There will be a maximum of 100 scores. // PLACE YOUR NAME HERE #include <iostream> using namespace std; typedef int GradeType[100]; // declares a new data type: // an integer array of 100 elements float findAverage...
It is time to play X-O. You should complete the program for this game. Part of...
It is time to play X-O. You should complete the program for this game. Part of this program is provided to you in two files: d4q2.py (it is complete, no need to be modified) and d4q2Lib.py (you have to complete). The annex has examples of messages displayed during the game. 1) The main program controls the game. a. It asks the user to start a game : if the response is not o or O, the program ends; If the...
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
C++ program Complete the following functions for linked list. You are not allowed to alter the...
C++ program Complete the following functions for linked list. You are not allowed to alter the names or the function prototypes. #ifndef _ULL #define _ULL #include <iostream> #include "nodeType.h" using namespace std; void initializeList(nodeType *&head, nodeType *&tail, int&count); //Initialize the list to an empty state. //Postcondition: head = NULL, tail = NULL, count = 0; bool isEmptyList(const nodeType *head) ; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print(const...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT