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