ICSE Class 10 Computer Applications Question 45 of 71

Arrays — Question 45

Back to all questions
45
Question

Question 39

The marks obtained by 50 students in a subject are tabulated as follows :

NameMarks
..
..
..

Write a program to input the names and marks of the students in the subject. Calculate and display:

(i) The subject average marks (subject average marks = subject total / 50 )

(ii) The highest marks in the subject and the name of the student. (The maximum marks in the subject are 100)

import java.util.Scanner;

public class KboatSDAMarks
{
    public static void main(String args[]) {
        final int TOTAL_STUDENTS = 50;
        Scanner in = new Scanner(System.in);
        
        String name[] = new String[TOTAL_STUDENTS];
        int marks[] = new int[TOTAL_STUDENTS];
        int total = 0;
        
        for (int i = 0; i < name.length; i++) {
            System.out.print("Enter name of student " + (i+1) + ": ");
            name[i] = in.nextLine();
            System.out.print("Enter marks of student " + (i+1) + ": ");
            marks[i] = in.nextInt();
            total += marks[i];
            in.nextLine();
        }
        
        double avg = (double)total / TOTAL_STUDENTS;
        System.out.println("Subject Average Marks = " + avg);
        int hIdx = 0;
        
        for (int i = 1; i < marks.length; i++) {
            if (marks[i] > marks[hIdx])
                hIdx = i;
        }
        
        System.out.println("Highest Marks = " + marks[hIdx]);
        System.out.println("Name = " + name[hIdx]);
    }
}
Output
BlueJ output of KboatSDAMarks.java
Answer