-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentsMarkAnalyzer.java
More file actions
94 lines (73 loc) · 2.7 KB
/
StudentsMarkAnalyzer.java
File metadata and controls
94 lines (73 loc) · 2.7 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.sarvesh.javabasics;
import java.util.Scanner;
public class StudentsMarkAnalyzer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number of Students : ");
int numberOfStudents = sc.nextInt();
double[] marksOfStudents = new double[numberOfStudents];
if (numberOfStudents <= 0) {
System.out.println("Try Again , Number of students can't be zero.");
}
System.out.print("Enter Student's Marks : ");
for(int i =0;i<numberOfStudents;i++) {
marksOfStudents[i] = sc.nextInt();
}
double total = calculateTotal(marksOfStudents);
double average = calculateAverage(total, numberOfStudents);
int highestIndex = findHighestIndex(marksOfStudents);
int lowestIndex = findLowestIndex(marksOfStudents);
int aboveAverageCount = countAboveAverage(marksOfStudents, average);
printReport(total, average, marksOfStudents, highestIndex, lowestIndex, aboveAverageCount);
sc.close();
}
public static double calculateTotal(double[] marks) {
double total = 0;
for (double mark : marks) {
total += mark;
}
return total;
}
public static double calculateAverage(double total, int count) {
return total / count;
}
public static int findHighestIndex(double[] marks) {
double max = marks[0];
int index = 0;
for (int i = 1; i < marks.length; i++) {
if (marks[i] > max) {
max = marks[i];
index = i;
}
}
return index;
}
public static int findLowestIndex(double[] marks) {
double min = marks[0];
int index = 0;
for (int i = 1; i < marks.length; i++) {
if (marks[i] < min) {
min = marks[i];
index = i;
}
}
return index;
}
public static int countAboveAverage(double[] marks, double average) {
int count = 0;
for (double mark : marks) {
if (mark > average) {
count++;
}
}
return count;
}
public static void printReport(double total, double average, double[] marks, int highestIndex, int lowestIndex, int aboveAverageCount) {
System.out.println("\n===== REPORT =====");
System.out.printf("Total Marks: %.2f%n", total);
System.out.printf("Average Marks: %.2f%n", average);
System.out.printf("Highest Marks: %.2f (Index %d)%n", marks[highestIndex], highestIndex);
System.out.printf("Lowest Marks: %.2f (Index %d)%n", marks[lowestIndex], lowestIndex);
System.out.printf("Students Above Average: %d%n", aboveAverageCount);
}
}