Program to Assign grades to a student using Nested If Else - GeeksforGeeks

28 May, 2021
Given an integer array marks, which comprises of marks scored by a student (out of 100) in different subjects, the task is to assign a grade to the student. The grade is found out by taking the percentage of the marks scored by the student. The percentage is calculated as: 
 
The grade is assigned using the following rules: 
Examples:  
Input: marks = { 25, 65, 46, 98, 78, 65 } Output: CInput: marks = { 95, 88, 98, 93, 92, 96 } Output: A  
 
Approach:  
Initialize a variable to sum all the marks scored by the student, total to 0.  
Initialize a variable to store the grade of the student, grade to ‘F’.  
First, we iterate through the marks array and find the total marks scored by the student.  
Then, we apply the formula described above to calculate the percentage.  
We then make a nested if else construct to assign proper grade to the student.  
For more on decision making and different types of decision making constructs, refer Decision Making in Java.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to assign grades to a student// using nested if-else#include<bits/stdc++.h>using namespace std; int main(){     // Store marks of all the subjects in an array    int marks[] = { 25, 65, 46, 98, 78, 65 };     // Max marks will be 100 * number of subjects    int len = sizeof(marks) / sizeof(marks[0]);    int max_marks = len * 100;     // Initialize student's total marks to 0    int total = 0;     // Initialize student's grade marks to F    char grade = 'F';     // Traverse though the marks array to find the sum.    for (int i = 0; i < len; i++)    {        total += marks[i];    }     // Calculate the percentage.    // Since all the marks are integer,    // typecast the calculation to double.    double percentage = ((double)(total) / max_marks) * 100;     // Nested if else    if (percentage >= 90)    {        grade = 'A';    }    else    {        if (percentage >= 80 && percentage <= 89)        {            grade = 'B';        }        else        {            if (percentage >= 60 && percentage <= 79)            {                grade = 'C';            }            else            {                if (percentage >= 33 && percentage <= 59)                {                    grade = 'D';                }                else                {                    grade = 'F';                }            }        }    }    cout << (grade) << endl;;} // This code is contributed by// Surendra_Gangwar
// Java program to assign grades to a student// using nested if-else class GFG {    public static void main(String args[])    {         // Store marks of all the subjects in an array        int marks[] = { 25, 65, 46, 98, 78, 65 };         // Max marks will be 100 * number of subjects        int max_marks = marks.length * 100;         // Initialize student's total marks to 0        int total = 0;         // Initialize student's grade marks to F        char grade = 'F';         // Traverse though the marks array to find the sum.        for (int i = 0; i < marks.length; i++) {            total += marks[i];        }         // Calculate the percentage.        // Since all the marks are integer,        // typecast the calculation to double.        double percentage            = ((double)(total) / max_marks) * 100;         // Nested if else        if (percentage >= 90) {            grade = 'A';        }        else {            if (percentage >= 80 && percentage <= 89) {                grade = 'B';            }            else {                if (percentage >= 60 && percentage <= 79) {                    grade = 'C';                }                else {                    if (percentage >= 33 && percentage <= 59) {                        grade = 'D';                    }                    else {                        grade = 'F';                    }                }            }        }         System.out.println(grade);    }}
# Python3 program to assign grades# to a student using nested if-else if __name__ == "__main__":     # Store marks of all the subjects    # in an array    marks = [25, 65, 46, 98, 78, 65 ]     # Max marks will be 100 * number    # of subjects    max_marks = len(marks)* 100     # Initialize student's total    # marks to 0    total = 0     # Initialize student's grade    # marks to F    grade = 'F'     # Traverse though the marks array    # to find the sum.    for i in range(len(marks)):        total += marks[i]     # Calculate the percentage.    # Since all the marks are integer,    percentage = ((total) /max_marks) * 100     # Nested if else    if (percentage >= 90):        grade = 'A'             else :        if (percentage >= 80 and            percentage <= 89) :            grade = 'B'                     else :            if (percentage >= 60 and                percentage <= 79) :                grade = 'C'                             else :                if (percentage >= 33 and                    percentage <= 59) :                    grade = 'D'                                     else:                    grade = 'F'             print(grade) # This code is contributed by ita_c
// C# program to assign grades to a student// using nested if-elseusing System; class GFG{public static void Main(){     // Store marks of all the subjects    // in an array    int []marks = { 25, 65, 46, 98, 78, 65 };     // Max marks will be 100 * number    // of subjects    int max_marks = marks.Length * 100;     // Initialize student's total marks to 0    int total = 0;     // Initialize student's grade marks to F    char grade = 'F';     // Traverse though the marks array to    // find the sum.    for (int i = 0; i < marks.Length; i++)    {        total += marks[i];    }     // Calculate the percentage.    // Since all the marks are integer,    // typecast the calculation to double.    double percentage = ((double)(total) /                                  max_marks) * 100;     // Nested if else    if (percentage >= 90)    {        grade = 'A';    }    else    {        if (percentage >= 80 && percentage <= 89)        {            grade = 'B';        }        else        {            if (percentage >= 60 && percentage <= 79)            {                grade = 'C';            }            else            {                if (percentage >= 33 && percentage <= 59)                {                    grade = 'D';                }                else                {                    grade = 'F';                }            }        }    }     Console.WriteLine(grade);}} // This code is contributed by Ryuga
<?php// PHP program to assign grades to a student// using nested if-else // Store marks of all the subjects in an array$marks = array(25, 65, 46, 98, 78, 65); // Max marks will be 100 * number of subjects$max_marks = sizeof($marks) * 100; // Initialize student's total marks to 0$total = 0; // Initialize student's grade marks to F$grade = 'F'; // Traverse though the marks array to find the sum.for ($i = 0; $i < sizeof($marks); $i++){    $total += $marks[$i];} // Calculate the percentage.// Since all the marks are integer,// typecast the calculation to double.$percentage = (($total) / $max_marks) * 100; // Nested if elseif ($percentage >= 90){    $grade = 'A';}else{    if ($percentage >= 80 && $percentage <= 89)    {        $grade = 'B';    }    else    {        if ($percentage >= 60 && $percentage <= 79)        {            $grade = 'C';        }        else        {            if ($percentage >= 33 && $percentage <= 59)            {                $grade = 'D';            }            else            {                $grade = 'F';            }        }    }} echo $grade . "\n"; // This code is contributed by Akanksha Rai?>
<script> // Javascript program to assign grades to a student// using nested if-else // Store marks of all the subjects in an arraylet marks=[25, 65, 46, 98, 78, 65]; // Max marks will be 100 * number of subjectslet max_marks = marks.length * 100;  // Initialize student's total marks to 0let total = 0; // Initialize student's grade marks to F        let grade = 'F';           // Traverse though the marks array to find the sum.        for (let i = 0; i < marks.length; i++) {            total += marks[i];        }           // Calculate the percentage.        // Since all the marks are integer,        // typecast the calculation to double.        let percentage            = ((total) / max_marks) * 100;           // Nested if else        if (percentage >= 90) {            grade = 'A';        }        else {            if (percentage >= 80 && percentage <= 89) {                grade = 'B';            }            else {                if (percentage >= 60 && percentage <= 79) {                    grade = 'C';                }                else {                    if (percentage >= 33 && percentage <= 59) {                        grade = 'D';                    }                    else {                        grade = 'F';                    }                }            }        }           document.write(grade);  // This code is contributed by rag2127</script>
C
 
ankthon
Akanksha_Rai
ukasp
SURENDRA_GANGWAR
rag2127
school-programming
Java Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Iterate HashMap in Java?
Iterate through List in Java
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array
Java program to count the occurrence of each character in a string using Hashmap
Iterate Over the Characters of a String in Java
Remove first and last character of a string in Java
How to Get Elements By Index from HashSet in Java?
Sorting in Java
Java Program to Write into a File