Write a program to create a single dimension array to store marks of 25 students. Then display the highest mark and also display how many students got the same highest mark.
import java.util.*;
class Marks {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a[] = new int[25];
int i; //loop
int j, d; //swapping elements sorting
int c=0; //To frequency
System.out.println("Enter marks of 25 students");
for (i = 0; i < 25; i++) {
a[i] = sc.nextInt();
}
// Stored 25 marks in an array
for (i = 0; i < 24; i++) {
for (j = 0; j < 24 - i; j++) {
if (a[j] < a[j + 1]) {
d = a[j];
a[j] = a[j + 1];
a[j + 1] = d;
// Elements swapped in descending order
}
}
}
Continue from here to: Find the highest marks and frequency of those students.
import java.util.*;
class Marks {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a[] = new int[25];
int i; // loop
int j, d; // swapping elements sorting
int c = 0; // To frequency
System.out.println("Enter marks of 25 students");
for (i = 0; i < 25; i++) {
a[i] = sc.nextInt();
}
// Stored 25 marks in an array
for (i = 0; i < 24; i++) {
for (j = 0; j < 24 - i; j++) {
if (a[j] < a[j + 1]) {
d = a[j];
a[j] = a[j + 1];
a[j + 1] = d;
// Elements swapped in descending order
}
}
}
// the highest marks and frequency of those students.
for (i = 0; i < 24; i++) {
if (a[0] == a[i]) {
c++;
}
}
System.out.println("The highest mark: " + a[0]);
System.out.println(c + " students got the same highest mark.");
}
}
Comments
Leave a comment