Question

In: Computer Science

All Code should be written in C: 1. A perfect number is defined as a number...

All Code should be written in C:
1. A perfect number is defined as a number whose proper divisors (factors not including the number itself) add up to the same number. For example, 28 is a perfect number because its perfect divisors are 1, 2, 4, 7, 14, which add up to 28. Write a C function called is_perfect that takes a since integer as input, and returns 1 if the number is perfect, or 0 otherwise.

2. Using the function you wrote in Q1, write a C program to print out all the perfect numbers between 1 and 10000. There are only 4 such numbers, the largest being 8128.

3. Write your own version of the strcpy() function, called mystrcpy(). This function should take two strings (char pointers) as input, and copy the second string into the first. Test that your function works properly.

4. Write a C function called strdelete() which takes a string (char pointer) and a character as input. The function should then remove any instance of the character from the string. This should be done by shifting all the other characters down one position in the array so the string becomes shorter. For example strdelete("hello world", 'o') will result in the string being "hell wrld".

Solutions

Expert Solution

Here is the completed code for each part. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

CODE FOR PART 1 (is_perfect method)

//returns 1 if n is a perfect number, else 0

int is_perfect(int n){

                int sum=0; //sum of all factors less than n

                //looping from i=1 to i=n/2

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

                                //checking if n is divisible by i

                                if(n%i==0){

                                               //adding i to sum

                                               sum+=i;

                                }

                }

                //at the end, if sum and n are same, number is perfect

                if(sum==n){

                                return 1;

                }else{

                                return 0;

                }

}

CODE FOR PART 2 (using is_perfect method to find perfect numbers under 10000)

#include<stdio.h>

//returns 1 if n is a perfect number, else 0

int is_perfect(int n){

                int sum=0; //sum of all factors less than n

                //looping from i=1 to i=n/2

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

                                //checking if n is divisible by i

                                if(n%i==0){

                                               //adding i to sum

                                               sum+=i;

                                }

                }

                //at the end, if sum and n are same, number is perfect

                if(sum==n){

                                return 1;

                }else{

                                return 0;

                }

}

int main(){

                printf("Perfect numbers between 1 and 10000\n");

                //printing perfect numbers between 1 and 10000

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

                                if(is_perfect(i)){

                                               printf("%d\n",i);

                                }

                }

                return 0;

}

CODE FOR PART 3 (mystrcpy method and testing)

#include<stdio.h>

//copies source string into dest, assuming dest have enough size to accomodate

//the source string

void mystrcpy(char* dest, char* source){

                int index=0;

                //looping as long as index is a valid index in source

                while(source[index]!='\0'){

                                //copying character at index from source to dest

                                dest[index]=source[index];

                                //updating index

                                index++;

                }

                //null terminating the dest string

                dest[index]='\0';

}

int main(){

                //creating two char arrays

                char destination[50];

                char source[50]="Hello World";

                //copying source to destination and displaying it

                mystrcpy(destination,source);

                printf("Copied string: %s\n",destination);

                return 0;

}

CODE FOR PART 4 (strdelete method and testing)

#include<stdio.h>

//deletes all occurrences of c from str

void strdelete(char* str, char c){

                int index=0;

                //looping as long as index is a valid index in str

                while(str[index]!='\0'){

                                //checking if current character is c

                                if(str[index]==c){

                                               int k=0;

                                               //starting from next element, copying all elements to one place left

                                               for(k=index;str[k+1]!='\0';k++){

                                                               str[k]=str[k+1];

                                               }

                                               //updating the end of string

                                               str[k]='\0';

                                }else{

                                               //moving to next index

                                               index++;

                                }

                               

                }

}

int main(){

                //creating a char array

                char text[50]="Hello World";

                //deleting all 'o's from text and displaying resultant string

                strdelete(text,'o');

                printf("String after deleting 'o': %s\n",text);

                return 0;

}


Related Solutions

All Code should be written in C: 1. Write a C program which prompts the user...
All Code should be written in C: 1. Write a C program which prompts the user to enter two integer values. Your program should then print out all numbers between 1 and 1000 that are divisible by both of those numbers. 2. Modify your program from question 1 such that the first 1000 numbers that are divisible by both numbers are printed out, instead of numbers up to 1000. 3. Using dynamic memory, allocate memory for an array of 100...
The code should be written in c++. It should be in only OpenGL // ***** ONLY...
The code should be written in c++. It should be in only OpenGL // ***** ONLY OpenGL PLEASE ******// write a program snippet that accepts the coordinates of the vertices of a rectangle centered around the origin, with edges that are parallel to the X and Y axes, and with aspect ratio W and converts it into a rectangle centered around the origin with aspect ratio of 1/W. The program has to figure W from the given points. You MUST...
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
Code should be written in C++ using Visual Studios Community This requires several classes to interact...
Code should be written in C++ using Visual Studios Community This requires several classes to interact with each other. Two class aggregations are formed. The program will simulate a police officer giving out tickets for parked cars whose meters have expired. You must include both a header file and an implementation file for each class. Car class (include Car.h and Car.cpp) Contains the information about a car. Contains data members for the following String make String model String color String...
Write a C++ program to print all the perfect numbers below a certain given number. A...
Write a C++ program to print all the perfect numbers below a certain given number. A perfect number is a number that that is equal too the sum of it's divisors other than itself . For example, 6 and 28 are all perfect numbers. 6 = ( 1+2 +3) 28 = (1+2+4+7+14) Make sure your program conforms to the following requirements: 1. Accept the upper limit from the user (as an integer). 2. Make sure the number is positive (at...
Module 07 Written Assignment - C-Diff Your written assignment for this module should be a 1-2...
Module 07 Written Assignment - C-Diff Your written assignment for this module should be a 1-2 page paper (not including title page and reference page) that describes the following: -You are caring for a patient with c-diff as part of your workload assignment. Discuss what c-diff is and how it is transmitted (how you can get it)? -What actions will you take as a nurse to protect yourself and the other patients on the unit when taking care of your...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision for each candidate Use the data from the example below. Your C program should take the...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision for each candidate Use the data from the example below. * Your C program should take...
I want this code to be written in c++. Take a string of length n as...
I want this code to be written in c++. Take a string of length n as an input from the user such that n>=8 and only lower-case letters (a-z) are allowed as valid string characters. Deleting a letter from the string removes all the occurrences of that letter. The objective is to find the longest possible string such that it is left with only two unique letters and no two consecutive characters in a string are the same. If there...
how to code in c# to get the total number of 1's as the numbers are...
how to code in c# to get the total number of 1's as the numbers are added in a Listbox in windows forms
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT