Calculate Student Result Using Else If Else in C

Definition:

The else if else statement is used to test multiple conditions one after another. It is useful when a program needs to make decisions based on different ranges of values. In this program, it is used to determine whether a student has passed or failed and to assign the appropriate grade based on the average marks.

Syntax:

if(condition)
{
    // statements
}
else if(condition)
{
    // statements
}
else if(condition)
{
    // statements
}
else
{
    // statements
}

Explanation:

This program accepts the marks of four subjects: Urdu, English, Maths, and Science. It calculates the total marks and average marks. First, it checks whether the student has passed in all subjects by ensuring each subject has at least 35 marks. Then, using the else if statement, it assigns a grade according to the average marks.

  • 90 to 100 → Grade A++
  • 75 to 89 → Grade A+
  • 60 to 74 → Grade A
  • 45 to 59 → Grade B
  • 35 to 44 → Grade C
  • Below 35 → Fail

🔹 Program: Calculate Student Result

Explanation: This program accepts marks of four subjects, calculates the total and average marks, checks whether the student has passed or failed, and displays the appropriate grade using the if and else if statements.


#include <stdio.h>
#include <conio.h>

main()
{
    int urd,eng,math,sci,total,avg;

    clrscr();

    printf("90 to 100 A++");
    printf("\n75 to 89 A+");
    printf("\n60 to 74 A");
    printf("\n45 to 59 B");
    printf("\n35 to 44 C");

    printf("\n\nUrdu : ");
    scanf("%d",&urd);

    printf("\nEnglish : ");
    scanf("%d",&eng);

    printf("\nMaths : ");
    scanf("%d",&math);

    printf("\nScience : ");
    scanf("%d",&sci);

    total = urd + eng + math + sci;
    avg = total / 4;

    printf("\nTotal Marks : %d",total);
    printf("\nAverage : %d",avg);

    if(urd >= 35 && eng >= 35 && math >= 35 && sci >= 35)
    {
        printf("\nResult : Pass");
    }
    else
    {
        printf("\nResult : Fail");
    }

    if(avg >= 90 && avg <= 100)
    {
        printf("\nGrade : A++");
    }
    else if(avg >= 75 && avg <= 89)
    {
        printf("\nGrade : A+");
    }
    else if(avg >= 60 && avg <= 74)
    {
        printf("\nGrade : A");
    }
    else if(avg >= 45 && avg <= 59)
    {
        printf("\nGrade : B");
    }
    else if(avg >= 35 && avg <= 44)
    {
        printf("\nGrade : C");
    }
    else
    {
        printf("\nResult : Fail");
    }

    getch();
}


Practice Exercises

Task 1: Write a C program to accept marks of Computer, Physics, and Chemistry. Calculate the total and average marks, then display the student's grade using the following conditions:

90 to 100: Grade A+
75 to 89: Grade A
60 to 74: Grade B
40 to 59: Grade C
Below 40: Fail

Goal: Learn how to calculate the average of marks and use the else if statement to assign grades.
💡 Hint: First calculate the total and average, then compare the average using else if conditions.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    int comp,phy,chem,total,avg;

    clrscr();

    printf("Computer : ");
    scanf("%d",&comp);

    printf("Physics : ");
    scanf("%d",&phy);

    printf("Chemistry : ");
    scanf("%d",&chem);

    total = comp + phy + chem;
    avg = total / 3;

    printf("\nTotal = %d",total);
    printf("\nAverage = %d",avg);

    if(avg >= 90)
    {
        printf("\nGrade : A+");
    }
    else if(avg >= 75)
    {
        printf("\nGrade : A");
    }
    else if(avg >= 60)
    {
        printf("\nGrade : B");
    }
    else if(avg >= 40)
    {
        printf("\nGrade : C");
    }
    else
    {
        printf("\nFail");
    }

    getch();
}

Output
Computer : 85
Physics : 90
Chemistry : 80

Total = 255
Average = 85
Grade : A
Explanation: The program accepts marks of three subjects, calculates the total and average, and uses the else if statement to determine the student's grade based on the average marks.