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...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Complete following function which receives an array of integers and the length of the array, and...
Complete following function which receives an array of integers and the length of the array, and then returns the sum of all the positive numbers in the array. For example, if an array that is passed to this function contains following numbers: -1, 2, 0, 3, 4, -3, 0, 2, 0, and then the return value of the function should be 11. Will this function be working correctly? Yes or No? int sumPositive(int a[],int length) { int s=0;     for(int...
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...
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.”....