Programming Examples

Java program to find grade based on marks using ternary operator


Write a Java program to determine a student's grade based on their score. The program should take the student's score as input and output the grade according to the following criteria:

A if the score is 90 or above

B if the score is between 80 and 89

C if the score is between 70 and 79

D if the score is between 60 and 69

F if the score is below 60

The program should use the ternary operator to determine the grade.

Solution

import java.util.Scanner;
public class StudentGrade 
{
    public static void main(String arg[]) 
	{
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the student's score: ");
        int score = sc.nextInt();
        String grade = (score >= 90) ? "A" :
						(score >= 80) ? "B" :
						(score >= 70) ? "C" :
						(score >= 60) ? "D" : "F";
        System.out.println("The student's grade is: " + grade);
    }
}
Output

Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2025 Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz