In: Computer Science
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".
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;
}