Question

In: Computer Science

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.

Solutions

Expert Solution

Raw_code:

#include<stdio.h> // including stdio.h header for scanf and printf functions
// perfect_num function for checking whether input number is perfect number or not
// if input is perfect returns number otherwise returns 0
int perfect_num(int number){
// temporary variable for stroing sum of factors
int temp_num = 0;
// for loop for dividing the number from 1 through number-1
for (int i = 1; i <number; i++){
// if ther number is perfectly divisible
if (number % i == 0)
// incrementing the temp_num with factor
temp_num += i;
}
// if statement for checking input number equals sum of factors
if (number == temp_num)
// if true returns input number
return number;
// else returns 0
else return 0;
}
// Q6 function takes an integer and integers array as input and returns an integer(count of perfect numbers)
int Q6(int Q6_input, int perfect[]){
// temporary variables declarations for storing perfect numbers count, index of array and
// return value of perfect_num function
int temp= 0, count =0;
// checking whether the input number is greather than 1
if (Q6_input > 1){
// for loop for iterating over the numbers upto to the given range
for (int i = 2; i <= Q6_input; i++){
// calling perfect_num on each number and storing return value in temp variable
temp = perfect_num(i);
// if return values not equals to 0
//if true (perfect number)
if (temp){
//assigning the perfect number into the array
perfect[count] = temp;
// incrementing array index and count of perfect numbers
count++;
}
}
// returning count of perfect numbers
return count;
}
else
printf("Invalid input!\n");
}
// main function
int main(){
// perfect is an array of 50 integers
int perfect[50];
// input variable to store user input
int input;
//askig user to enter input through printf function
printf("Enter the number : ");
// taking input through scanf function
scanf("%d", &input);
// calling Q6 function by passing input and perfect array as arguments
int count = Q6(input, perfect);
// printing count of perfect numbers
printf("perfect numbers count : %d \n", count);
// printing list of perfect numbers
printf("The numbers are : ");
// iterating over array for printing perfect numbers
for (int i = 0; i < count; i++){
printf("%d\t", perfect[i]);
}
}


Related Solutions

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: 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 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
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: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
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: Complete the int Q7a(intQ7_input) function takes only 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()) Complete the following functions using C programming language: 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 arrays...
Look up the man pages for the following string functions from the C standard library, and...
Look up the man pages for the following string functions from the C standard library, and write implementations of each: strstr() Please explain the work please, thank you
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...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal). The function should allocate storage from...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C program using system call I/O to determine how many lines there are in a text file.
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from the implementation: * reset * intersection * difference Set Set::intersection(Set& s){ Set r; // find intersection return r; } Set Set::difference(Set& s){ Set r; // find difference return r; } void Set::reset(int c){ // increase the capacity and clear the data } driver program int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT