Write a program that accept marks in 4 subjects, the program then compute and output total, average, and grade and remark based on the following grading system. a) Use if else b) Use switch case Marks Grade Remark 70-100 A Excellent 60-69 B Good 50-59 C Satisfactory 40-49 D Pass 0-39 F Fail

Computer Science

ScanSolve AI Answer

Question 1: Write a program that accepts marks in 4 subjects, computes total, average, and grade using if-else.

# Program using if-else
def calculate_grade(marks):
    total = sum(marks)
    average = total / len(marks)
    
    if average >= 70:
        grade = 'A'
        remark = 'Excellent'
    elif average >= 60:
        grade = 'B'
        remark = 'Good'
    elif average >= 50:
        grade = 'C'
        remark = 'Satisfactory'
    elif average >= 40:
        grade = 'D'
        remark = 'Pass'
    else:
        grade = 'F'
        remark = 'Fail'
    
    return total, average, grade, remark

marks = [float(input("Enter marks for subject {}: ".format(i+1))) for i in range(4)]
total, average, grade, remark = calculate_grade(marks)
print(f"Total: {total}, Average: {average}, Grade: {grade}, Remark: {remark}")

Question 2: Write a program that accepts marks in 4 subjects, computes total, average, and grade using switch-case.

# Program using switch-case (simulated with dictionary)
def calculate_grade(marks):
    total = sum(marks)
    average = total / len(marks)
    
    def get_grade_remark(avg):
        switcher = {
            (70, 100): ('A', 'Excellent'),
            (60, 69): ('B', 'Good'),
            (50, 59): ('C', 'Satisfactory'),
            (40, 49): ('D', 'Pass'),
            (0, 39): ('F', 'Fail'),
        }
        for key in switcher:
            if key[0] <= avg <= key[1]:
                return switcher[key]
    
    grade, remark = get_grade_remark(average)
    return total, average, grade, remark

marks = [float(input("Enter marks for subject {}: ".format(i+1))) for i in range(4)]
total, average, grade, remark = calculate_grade(marks)
print(f"Total: {total}, Average: {average}, Grade: {grade}, Remark: {remark}")
Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question