-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradeCalculator.java
51 lines (41 loc) · 1.58 KB
/
GradeCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.Scanner;
public class calculateGrade {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of subjects:");
int numberOfSubjects = scanner.nextInt();
int totalMarks = 0;
for (int i = 1; i <= numberOfSubjects; i++) {
System.out.println("Enter marks obtained in subject " + i + " (out of 100):");
int marks = scanner.nextInt();
if (marks < 0 || marks > 100) {
System.out.println("Invalid marks entered. Marks should be between 0 and 100.");
return;
}
totalMarks += marks;
}
double averagePercentage = (double) totalMarks / numberOfSubjects;
String grade = calculateGrad(averagePercentage);
// Display results
System.out.println("\nResults:");
System.out.println("Total Marks: " + totalMarks);
System.out.println("Average Percentage: " + averagePercentage + "%");
System.out.println("Grade: " + grade);
scanner.close();
}
private static String calculateGrad(double averagePercentage) {
if (averagePercentage >= 90) {
return "A+";
} else if (averagePercentage >= 80) {
return "A";
} else if (averagePercentage >= 70) {
return "B";
} else if (averagePercentage >= 60) {
return "C";
} else if (averagePercentage >= 50) {
return "D";
} else {
return "F";
}
}
}