KnowledgeBoat Logo

Computer Applications

In an entrance examination, students have been appeared in English, Maths and Science papers. Write a program to calculate and display average marks obtained by all the students. Take number of students appeared and marks obtained in all three subjects by every student along with the name as inputs.
Display the name, marks obtained in three subjects and the average of all the students.

Java

Java Nested for Loops

42 Likes

Answer

import java.util.Scanner;

public class KboatStudentMarks
{
    public void studentMarks() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of students: ");
        int studentCount = in.nextInt();
        
        String names[] = new String[studentCount];
        int engMarks[] = new int[studentCount];
        int sciMarks[] = new int[studentCount];
        int mathsMarks[] = new int[studentCount];
        double avgMarks[] = new double[studentCount];
        double totalMarks = 0.0;
        
        for (int i = 0; i < studentCount; i++) {
            System.out.println("Enter details of student " + (i + 1));
            System.out.print("Name: ");
            in.nextLine();
            names[i] = in.nextLine();
            System.out.print("Marks in English: ");
            engMarks[i] = in.nextInt();
            System.out.print("Marks in Science: ");
            sciMarks[i] = in.nextInt();
            System.out.print("Marks in Maths: ");
            mathsMarks[i] = in.nextInt();
            avgMarks[i] = (engMarks[i] + sciMarks[i] + mathsMarks[i]) / 3.0;
            totalMarks += avgMarks[i];
        }
        
        System.out.println();
        
        for (int i = 0; i < studentCount; i++) {
            System.out.println("Details of student " + (i + 1));
            System.out.println("Name: " + names[i]);
            System.out.println("English: " + engMarks[i]);
            System.out.println("Science: " + sciMarks[i]);
            System.out.println("Maths: " + mathsMarks[i]);
            System.out.println("Average: " + avgMarks[i]);
        }
        
        double classAvg = totalMarks / studentCount;
        System.out.println("\nAverage of all students is " + classAvg);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of In an entrance examination, students have been appeared in English, Maths and Science papers. Write a program to calculate and display average marks obtained by all the students. Take number of students appeared and marks obtained in all three subjects by every student along with the name as inputs. Display the name, marks obtained in three subjects and the average of all the students.

Answered By

15 Likes


Related Questions