In: Computer Science
Write a Java program that will ask the user for his/her mark (numerical integer mark) and then convert this numerical mark into letter grades. The following is a guideline to the grading scale used. The numeric range within parenthesis maps to the preceding letter grade. If the user gave you a number greater than 100 or less than 0, you should print a message that the input is invalid. In this code, DO NOT USE && OPERATOR. You should use if-else.
Grades Mapping
A+ (90 to100)
A (80 to 89)
B+ (75 to 79)
B (70 to 74)
C+ (65 to 69)
C (60 to 64)
D+ (55 to 59)
D (50 to 54)
E (40 to 49)
F (0 to 39)
Required program in java -->
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter marks"); //ask user for
input
int marks = sc.nextInt(); //read input from user
if((marks+10)>=100){ //if marks+10 is smaller than
100
if(marks<=100){ //if marks is less than or equal to
100
System.out.println("A+"); //then print A+
}
else{ //else
System.out.println("Invalid Input"); //print
invalid
}
}
else if((marks+10)>=90){
System.out.println("A");
}
else if((marks+5)>=80){
System.out.println("B+");
}
else if((marks+5)>=75){
System.out.println("B");
}
else if((marks+5)>=70){
System.out.println("C+");
}
else if((marks+5)>=65){
System.out.println("C");
}
else if((marks+5)>=60){
System.out.println("D+");
}
else if((marks+5)>=55){
System.out.println("D");
}
else if((marks+10)>=50){
System.out.println("E");
}
else if((marks+40)>=40){
System.out.println("F");
}
}
}
we cannot use && operators here, so we have used + operator to check whether the given input falls in the criteria. And according to marks, we have provided the resultant grade.
so, if user enters 55
o/p -> D+