Write a Java program to display the grade of a student along with a student along with a message. The grade and the message to be displayed as per percentage are given above:
Create this program using the if-else if construct.
Answers
Answer:
This program calculates the grade of a student based on the marks entered by user in each subject. Program prints the grade based on this logic.
If the average of marks is >= 80 then prints Grade ‘A’
If the average is <80 and >=60 then prints Grade ‘B’
If the average is <60 and >=40 then prints Grade ‘C’
else prints Grade ‘D’
To understand this Program you should have the knowledge of following concepts of Java:
Java For Loop
Arrays in Java
if..else-if in Java
Example: Program to display the grade of student
import java.util.Scanner;
public class JavaExample
{
public static void main(String args[])
{
/* This program assumes that the student has 6 subjects,
* thats why I have created the array of size 6. You can
* change this as per the requirement.
*/
int marks[] = new int[6];
int i;
float total=0, avg;
Scanner scanner = new Scanner(System.in);
for(i=0; i<6; i++) {
System.out.print("Enter Marks of Subject"+(i+1)+":");
marks[i] = scanner.nextInt();
total = total + marks[i];
}
scanner.close();
//Calculating average here
avg = total/6;
System.out.print("The student Grade is: ");
if(avg>=80)
{
System.out.print("A");
}
else if(avg>=60 && avg<80)
{
System.out.print("B");
}
else if(avg>=40 && avg<60)
{
System.out.print("C");
}
else
{
System.out.print("D");
}
}
}
Output:
Enter Marks of Subject1:40
Enter Marks of Subject2:80
Enter Marks of Subject3:80
Enter Marks of Subject4:40
Enter Marks of Subject5:60
Enter Marks of Subject6:60
The student Grade is: B
Answer:
Explanation:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
float percentage;
Scanner input=new Scanner(System.in);
System.out.println("Full Marks : 80");
System.out.println("Marks of 7 subjects : (80 x 7) = 560");
System.out.println("____________________________________________________");
System.out.println ("Enter marks of SST");
a=input.nextInt();
System.out.println ("Enter marks of L2");
b=input.nextInt();
System.out.println ("Enter marks of ENG");
c=input.nextInt();
System.out.println ("Enter marks of COMP");
d=input.nextInt();
System.out.println ("Enter marks of SCI");
e=input.nextInt();
System.out.println ("Enter marks of L3");
f=input.nextInt();
System.out.println ("Enter marks of MATH");
g=input.nextInt();
percentage=(a+b+c+d+e+f+g)*100/560;
if (percentage>=80)
{
System.out.println("Your percentage is : "+percentage+"%");
System.out.println("Your grade : A+. EXCELLENT JOB!");
}
else if (percentage>=75)
{
System.out.println("Your percentage is : "+percentage+"%");
System.out.println("Your grade : A. WELL DONE!");
}
else if (percentage>=70)
{
System.out.println("Your percentage is : "+percentage+"%");
System.out.println("Your grade : B. SATISFACTORY PERFORMANCE!");
}
else if (percentage>=66.5)
{
System.out.println("Your percentage is : "+percentage+"%");
System.out.println("Your grade : C. YOU CAN DO BETTER!");
}
else if (percentage>=50)
{
System.out.println("Your percentage is : "+percentage+"%");
System.out.println("Your grade : D. PERFORMANCE NEEDS IMPROVEMENT!");
}
else
{
System.out.println("Your percentage is : "+percentage+"%");
System.out.println("Your grade : E. YOU FAILED! STUDY HARD!");
}
}
}