Pages

Saturday, January 25, 2025

Write a program in C for grade calculation using if…else if ladder and switch Statement. Accept marks of 3 subjects calculate total and based on it calculate Grade.

FLOWCHART
  • Start
  • Input marks for 3 subjects (sub1, sub2, sub3)
  • Calculate Total Marks (total = sub1 + sub2 + sub3)
  • Calculate Average Marks (average = total / 3)
  • Check if all subjects have marks >= 33 (to pass)
    • If YES, proceed to calculate the grade.
    • If NO, print "FAILED" and stop.
  • Check the grade based on the average using if-else conditions:
    • If average >= 80, assign grade 'A'.
    • Else if average >= 65, assign grade 'B'.
    • Else if average >= 45, assign grade 'C'.
    • Else if average >= 33, assign grade 'D'.
    • Else assign grade 'F' (failed).
  • Display total marks, average, and grade.
  • End


    #include <stdio.h>

    int main() {
        int sub1, sub2, sub3, total;
        float average;
        char grade;
        
        // Input marks for 3 subjects
        printf("Enter marks of 1st Subject: ");
        scanf("%d", &sub1);
        
        printf("\nEnter marks of 2nd Subject: ");
        scanf("%d", &sub2);
        
        printf("\nEnter marks of 3rd Subject: ");
        scanf("%d", &sub3);
        
        // Total Marks and Average
        total = sub1 + sub2 + sub3;
        average = total / 3.0;
        
        // Check if passed
        if (sub1 >= 33 && sub2 >= 33 && sub3 >= 33) {
            printf("\nTotal Marks: %d", total);
            printf("\nAverage Marks: %.2f", average);
            
            // Grade calculation using if...else if ladder
            if (average >= 80) {
                grade = 'A';
            } else if (average >= 65) {
                grade = 'B';
            } else if (average >= 45) {
                grade = 'C';
            } else if (average >= 33) {
                grade = 'D';
            } else {
                grade = 'F'; // F for fail if below 33
            }
            
            // Display grade using switch statement
            switch(grade) {
                case 'A':
                    printf("\nStudent has A grade.");
                    break;
                case 'B':
                    printf("\nStudent has B grade.");
                    break;
                case 'C':
                    printf("\nStudent has C grade.");
                    break;
                case 'D':
                    printf("\nStudent has D grade.");
                    break;
                case 'F':
                    printf("\nStudent has FAILED, advised to re-take exam.");
                    break;
                default:
                    printf("\nInvalid grade.");
            }
        } else {
            printf("\nStudent has FAILED, advised to re-take exam.");
        }

        return 0;
    }
  • No comments:

    Post a Comment

    Stack using Linked List – Partial (University Exam Topic)

     #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...