Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. Write below programs. a. Using a do-while statement, write a C program to acc

ID: 3721875 • Letter: 4

Question

4. Write below programs. a. Using a do-while statement, write a C program to accept a grade. The program should request a grade continuously as long as a valid grade is entered. An invalid grade is any grade less than 0 or greater than 100. After a valid grade has been entered, your program should display its value. (10 pts) b. Modify the program written for a. so that the user is alerted when an invalid grad has been entered. (10 pts) c. Modify the program written in b. so that it allows the user to exit the program by entering the number 999. (10 pts) -END-

Explanation / Answer

1. I have used a helper function is_valid_grade() to check if the entered grade is valid or not.

C Program:

#include <stdio.h>
#include <stdlib.h>

int is_valid_grade(int grade) {
if(grade >= 0 && grade <= 100) // Range of valid grades
return 1; // True
return 0; // False
}

int main()
{
int grade;
do {
printf("Enter grade: ");
scanf("%d", &grade);
if(is_valid_grade(grade)) // If entered grade is valid, print it
printf("The grade is %d ", grade);
} while(is_valid_grade(grade)); // Keep repeating until the grade is valid

return 0;
}

2. Add an else statement for invalid grades. The modified C program for this scenario is:

#include <stdio.h>
#include <stdlib.h>

int is_valid_grade(int grade) {
if(grade >= 0 && grade <= 100) // Range of valid grades
return 1; // True
return 0; // False
}

int main()
{
int grade;
do {
printf("Enter grade: ");
scanf("%d", &grade);
if(is_valid_grade(grade))
printf("The grade is %d ", grade);
else
printf("The entered grade is invalid. "); // Alert user in case of invalid grade
} while(is_valid_grade(grade));

return 0;
}

3. Add an if statement checking if the input is 999 and exiting the program.

#include <stdio.h>
#include <stdlib.h>

int is_valid_grade(int grade) {
if(grade >= 0 && grade <= 100) // Range of valid grades
return 1; // True
return 0; // False
}

int main()
{
int grade;
do {
printf("Enter grade: ");
scanf("%d", &grade);
if(grade == 999) // If the input is 999, exit the program by executing return 0
return 0;
if(is_valid_grade(grade))
printf("The grade is %d ", grade);
else
printf("The entered grade is invalid. ");
} while(is_valid_grade(grade));

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote