-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.java
More file actions
60 lines (49 loc) · 2 KB
/
Copy pathtask2.java
File metadata and controls
60 lines (49 loc) · 2 KB
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
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
public class StudentGradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("STUDENT GRADE CALCULATOR");
System.out.println("-----------------------");
// Input number of subjects
System.out.print("Enter the number of subjects: ");
int numSubjects = scanner.nextInt();
// Input marks for each subject
int[] marks = new int[numSubjects];
int totalMarks = 0;
for (int i = 0; i < numSubjects; i++) {
System.out.printf("Enter marks for subject %d (out of 100): ", i + 1);
marks[i] = scanner.nextInt();
// Validate input
while (marks[i] < 0 || marks[i] > 100) {
System.out.println("Invalid marks! Please enter marks between 0 and 100.");
System.out.printf("Enter marks for subject %d (out of 100): ", i + 1);
marks[i] = scanner.nextInt();
}
totalMarks += marks[i];
}
// Calculate average percentage
double averagePercentage = (double) totalMarks / numSubjects;
// Determine grade
String grade;
if (averagePercentage >= 90) {
grade = "A+";
} else if (averagePercentage >= 80) {
grade = "A";
} else if (averagePercentage >= 70) {
grade = "B";
} else if (averagePercentage >= 60) {
grade = "C";
} else if (averagePercentage >= 50) {
grade = "D";
} else {
grade = "F";
}
// Display results
System.out.println("\nRESULTS");
System.out.println("-------");
System.out.println("Total Marks: " + totalMarks);
System.out.printf("Average Percentage: %.2f%%\n", averagePercentage);
System.out.println("Grade: " + grade);
scanner.close();
}
}