Question

In this example you are allowed to use from the C standard library only functions for...

In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf())

Complete the following functions using C programming language:

A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal to some integer Q6_input(Q6_input> 1).

  • You have to print using this format:
  • perfect is an array that you need to add into it any perfect number you, which means at the end of this function, the perfect[] array should hold all the found perfect numbers in the range you do not need to return perfect because arrays are already passed by reference. so, modifying them will automatically reflect in the main calling function.
  • The array perfect[]should hold all the perfect numbers you find.
  • The function should also return the total count of the perfect numbers you found.

Note: Assume that x and y are two positive integers. Then x is a factor of y if the remainder of the division of y by x is 0. For instance, 5 is a factor of 15, but not of 36.

For example: if Q6_inputis 10 then the only perfect number you will find is 6. Accordingly, perfect[0] should be equal 6 and the function should return 1 as your count.

Homework Answers

Answer #1

Code:

#include<stdio.h>
int Q6(int Q6_input, int *perfect)
{
   int i,j,count=0,factSum;/*Declaring variables*/
   for(i=6;i<=Q6_input;i++)
   {/*This loop iterates for 6 to Q6_input
   because 6 is the first perfect number*/
       factSum=0;/*For every iteration we make factors sum to 0*/
       for(j=1;j<i;j++)
       {
           /*This loop iterates for 1 to i-1 values*/
           if(i%j==0)
           {/*If i is divisible by j then we add it to the factsum*/
               factSum+=j;
           }  
       }
       if(factSum==i)
       {/*If factsum is equals to the i
           then we append it to the perfect array*/
           *(perfect+count)=i;
           count++;
           /*Here we increase the count*/
       }
   }
   return count;/*Here we return the count of the perfect numbers*/
}
int main()
{
   int num,i,perfect[100],count;/*Declaring variables*/
   printf("Enter a number to print perfect numbers below a given number:");
   scanf("%d",&num);/*Here we read the input from the user*/
   count=Q6(num,perfect);/*Here we call the function by passing the num and array reference*/
   if(count==0)
   {/*If count is 0 means no perfect numbers in the range*/
       printf("There are no perfect numbers in the given range");
   }
   else
   {/*IF count is greater than 0 we print the perfect numbers*/
       printf("Perfect numbers in the given range are:\n");
       for(i=0;i<count;i++)
       {
           printf("%d\n",perfect[i]);
       }
   }
  
  
}

Output:

Indentation:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: a) Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1018, -2 and 46 are not. Complete the intQ2(intQ2_input) function that takes an input integer parameter and returns 1 if the number is...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Calculate the value of π from the infinite series: π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + … Complete the double Q4(intQ4_input) function which reads a positive integer Q4_input as an input parameter and calculates the value of π by adding up the first...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: intQ1_for() intQ1_while() intQ1_do() To compute the sum of all numbers that are multiples of 4, between 30 and 1000, in 3 different ways: with a for loop, a while loop and a do-while loop, accordingly. After each loop print the value. Return the total sum at the end of each...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following function using C programming language: Complete the function intQ3(floatQ3_input) that takes a student’s average as an input, which is a floating-point value, and returns: 4 if the average is in the range 90-100, 3 if it is in the range 80-89, 2 if it is in the range 70-79, 1 if it is in the...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...