Consider an array MARKS[20][5] which stores the marks obtained by 20 students in 5 subjects. Now write a program to
(a) find the average marks obtained in each subject.
(b) find the average marks obtained by every student.
(c) display the scores obtained by every student and grades according to the average marks obtained by every student
SOLUTION CODE FOR THE ABOVE QUESTION
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// menu for the program
System.out.println("1. Find the average marks obtained in each subject.");
System.out.println("2. Find the average marks obtained by every student.");
System.out.println("3. Display the scores obtained by every student \n" +
"and grades according to the average marks obtained by every student");
System.out.println("4. Exit program");
System.out.print("Enter the operation you want to perform: ");
int option = sc.nextInt();
//Populated array called MARKS for functionality testing
int[][] MARKS = {
{80, 98, 75, 58,95},
{52, 38, 64, 36,28},
{58, 56, 82, 92,46},
{46, 16, 12, 50,24},
{88, 38, 81, 54,46},
{50, 54, 62, 66,52},
{94, 96, 98, 86,84},
{60, 16, 62, 14,52},
{95, 90, 94, 66,82},
{90, 42, 85, 87,65},
{82, 94, 75, 82,96},
{98, 38, 64, 36,32},
{94, 56, 82, 92,48},
{78, 58, 68, 74,44},
{52, 16, 34, 10,26},
{76, 88, 72, 58,46},
{70, 96, 98, 82,76},
{84, 26, 62, 22,34},
{92, 90, 88, 76,82},
{78, 42, 71, 83,65}
};
if(option==1)
{
for(int i = 0; i<5;i++)
{
int subject_total = 0;
for(int j = 0; j< 20; j++)
{
subject_total = subject_total + MARKS[j][i];
}
System.out.println("The average mark for Subject "+(i+1)+" = "+(subject_total/20));
}
}
else if(option==2)
{
for(int i = 0; i<20;i++)
{
int student_total_marks = 0;
for(int j = 0; j<5; j++)
{
student_total_marks = student_total_marks + MARKS[i][j];
}
System.out.println("Average mark for student "+(i+1)+" = "+(student_total_marks/5));
}
}
else if(option==3)
{
for(int i = 0; i<20;i++)
{
int student_total_marks = 0;
System.out.println("Student "+(i+1)+" scores:");
for(int j = 0; j<5; j++)
{
System.out.print("Subject "+(j+1)+" = "+MARKS[i][j]+", ");
student_total_marks = student_total_marks + MARKS[i][j];
}
float average = student_total_marks/5;
System.out.print("Average: "+average);
//Determine the grade based on the following criteria
if(average > 69 & average < 100)
{
System.out.println("Grade A");
}
else if(average > 59 & average < 70)
{
System.out.println("Grade B");
}
else if(average > 49 & average < 60)
{
System.out.println("Grade C");
}
else if(average > 39 & average < 50)
{
System.out.println("Grade D");
}
else
{
System.out.println(" Grade E");
}
System.out.println("");
}
}
else if(option==4)
{
System.out.println("Program exited successfully");
}
else{
System.out.println("Invalid option please try again");
}
}
}
SAMPLE PROGRAM OUTPUTS
Comments
Leave a comment