-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentGradeCalculator.java
More file actions
55 lines (46 loc) · 1.8 KB
/
StudentGradeCalculator.java
File metadata and controls
55 lines (46 loc) · 1.8 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
import java.util.Scanner;
public class StudentGradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for the number of subjects
System.out.print("Enter the number of subjects: ");
int numSubjects = scanner.nextInt();
// Array to store marks for each subject
int[] marks = new int[numSubjects];
int totalMarks = 0;
// Input marks for each subject
for (int i = 0; i < numSubjects; i++) {
System.out.print("Enter marks for subject " + (i + 1) + " (out of 100): ");
marks[i] = scanner.nextInt();
// Validate the input marks
if (marks[i] < 0 || marks[i] > 100) {
System.out.println("Invalid marks! Please enter marks between 0 and 100.");
i--; // Ask again for the same subject
} else {
totalMarks += marks[i];
}
}
// Calculate average percentage
double averagePercentage = (double) totalMarks / numSubjects;
// Determine grade
char grade;
if (averagePercentage >= 90) {
grade = 'A';
} else if (averagePercentage >= 75) {
grade = 'B';
} else if (averagePercentage >= 50) {
grade = 'C';
} else if (averagePercentage >= 35) {
grade = 'D';
} else {
grade = 'F';
}
// Display results
System.out.println("\n--- Results ---");
System.out.println("Total Marks: " + totalMarks);
System.out.println("Average Percentage: " + averagePercentage + "%");
System.out.println("Grade: " + grade);
// Close scanner
scanner.close();
}
}