VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-assign-grades-to-a-student-using-nested-if-else/

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


  • Courses
  • Tutorials
  • Interview Prep

Program to Assign grades to a student using Nested If Else

Last Updated : 11 Jul, 2025

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: 
 

👁 Image


The grade is assigned using the following rules:
 

PercentageGrade
90 and aboveA
80 to 89B
60 to 79C
33 - 59D
below 33F


Examples: 
 

Input: marks = { 25, 65, 46, 98, 78, 65 } 
Output: C
Input: marks = { 95, 88, 98, 93, 92, 96 } 
Output:
 


 


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: 
 


Output: 
C

 
Comment