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’
// Write a Program to display the grade of student
import java.util.Scanner;
public class Grade1
{
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
sc = new Scanner(System.in);
for(i=0;
i<6; i++) {
System.out.print("Enter Marks of
Subject"+(i+1)+":");
marks[i] = sc.nextInt();
total
= total + marks[i];
}
sc.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");
}
}
}
·
Explanation:- Using Scanner class to
read user input from the keyboard.
·
Using Scanner class to read a File on the disk.
·
Using Scanner class to read from a String.
Output:-
Enter Marks of Subject1:80
Enter Marks of Subject2:45
Enter Marks of Subject3:67
Enter Marks of Subject4:89
Enter Marks of Subject5:45
Enter Marks of Subject6:78
The student Grade is: B
0 comments:
Post a Comment