Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

here is the code for question 5: here is the code for question 7: Here is the main question which is question 6 and I

image text in transcribed

here is the code for question 5:image text in transcribed

image text in transcribed

here is the code for question 7:image text in transcribedimage text in transcribed

Here is the main question which is question 6 and I am asking the code to be in C code language and not in python or C++ and I am just for clarity.

image text in transcribed

5. Implement a function that takes an integer n as a parameter and checks whether n is a perfect number using the function defined in Question 5. If n is a perfect number, it returns 1, otherwise 0 (a number is a perfect number if it is equal to sum of its divisors excluding the number itself). #include 1 2 6 9 11 FB 3 /*function to check perfect number or not* 4 int isPerfect(int num) 5. { int loop, sum; 7 8 for(loop1; loop num; Loop++) { 10 if(num loop) sum-Loop; 12 } 13 14 if(sum num) 15 return 1; "Perfect Number/ 16 else 17 return; "Not Perfect Number */ 18 } 19 20 21 int main() 22. { 23 int num, loop; 24 int sum; 26 printf("Enter an integer number: "); 27 scanf("%d",&num); 28 30 31 32 33 34 35 } if(isPerfect(num)) printf("YES");*If the given integer is a perfect number print YES*/ else printf("NO"); /*If the given integer is not a perfect number print NO*/ return 0; 7. Define a structure called Date that contains 3 members: year, month, and day. Then: a. Implement a function called getNextDate that takes a Date parameter and returns a Date which is 1 day after the date of the input parameter. For example, if the function's input is equal to 1/6/2022, the function must return 1/7/2022. (Do not consider leap years.) b. Implement a function that takes two parameters of the Date type and compares them. i. If the two parameters refer to the same date, the function returns zero, ii. If the first parameter is a date after the date of the second parameter, it returns +1, iii. If the first parameter is the date before the date of the second parameter, it returns -1. First of all C file: int year; 1 #include 2 // Date Structure. 3 struct Date 4 // Data Members that show day, month and year. 5 int day 6 int month; 7 8 9 // Set the date with month day and year and return the Date Object. 10 struct Date setDate(int month, int day, int year){ 11 // Set the date object d 12 struct Date d; 13 // Set values. 14 d. day-day; 15 d. month month; 16 d.year-year 17 // Return Date Object. 18 return di 19] 20 // Get the number of days per month by month number 21 int getDaysByMonth(int month) { 22 // If month number is 1,3,5,7,8,10,12 => return 31 23 // If month number is 2(February) => return 28 =>Not considering Leap Year 24 // Else Return 30. 25 switch (month) { 26 case 1: 27 case 3: 28 case 5: 29 case 7: 30 case 8: 31 case le: 32 case 12:( 33 return 31; 34 } 35 case 2: 36 return 28; 37 } 38 default: 39 return 30 40 1 41 } 42 ) 43 // Get the next date function. 44 struct Date getNextDate(struct Date d) { 45 printf(" Getting Next Date : "); 46 // Create a new Date Object nextDate. 47 struct Date nextDate; 48 49 // First of all set the values as the d object of Date. 50 nextDate.day=d. day 51 nextDate.month=d.month; 52 nextDate.year=d.year; 53 54 // if the day is the last day of month then we need to set nextdate.day to 1 55 // And then Check if the month is the last month that is 12 then set month to 1 and set year to next 56 // Else remain them as it is. 57 if(d.day == getDaysByMonth(d. month)) { 58 nextDate.day-1; 59 if(d.month=-12) 60 nextDate.month=2; 61 nextDate.year.d. year+1; 62 } 63 else{ 64 nextDate.month=d.month+1; 65 } 66 1 67 else 68 nextDate.day 1+d. day GS GG > 67 else { GB nextDate.day-1 d. day G9 1 70 // Finally Return the next Date. 71 return nextDate 72 73 // Fucntion to compare 2 dates. 74 int compareDate(struct Date di, struct Date d2) 75 // First of all check if the two years are same or not. 76 // if 2 months are same and if two dates are same then return e. 77 // If Years are sane and monthi>month2 => return 1 78 // If Months and years are same then day1>day2->return 1 79 // Else in the case we need to return 2. 90 if(di.year-d2 year) 81 if(d.month 2 month) 92 if(d1.day-d2.day) 83 return 8 84 > 85 else{ 96 if(d1.day>d2.day) 87 return 1 88 > 29 else 90 return 2; 91 } 92 > 93 } 94 else 95 if(d1.month>d2.month) { 96 return 1; 97 > 98 else { 99 return 2 100 ) 101 } 102 } 100 else 104 if(d1 year>d2 year) { 105 return 11 100 1 107 else 100 return 2: 109 } 110 } 111) 112 113 // Print the date int the format mm/dd/yyyy. 114 // By taking the date object. 115 void printDate(struct Date d) { 116 printf("Date : X1/Xd/%d ", d.month.d. day, d.year); d 117) 118 // Main Function Driver. 119 int main() { 120 // Set a date object d => with date as 12/31/2022 121 struct Date desetDate(12,31,2822); 122 // Print the date 122 printDate(d); 124 // Get the next date in the same obejct. 125 d-getNextDate(d); 126 // Print the next Date 127 printDate(d); 128 129 // No printing the two dates with object di and d2. 130 printf(" The Two dates to compare are : "); 131 struct Date dl-setDate(11,30,2022); 132 printDate(dl): 133 struct Date d2 setDate(12, 31,2822); 134 printDate(d2); 135 130 // Output the answer of comparing 2 dates di and d2. 137 printf("Comparing Date : xd ", compareDate(di,d2)); 132 139 return @ 140 ) 141 6. Using the two functions defined for Question 5 and Question 7, implement a program that prints all the perfect numbers less than 10,000 in descending order

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

If 2 (20+2) + 4 = 30, find 20+2.

Answered: 1 week ago