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) find the number of students who have scored below 50 in their average.
(d) display the scores obtained by every student.
SOLUTION CODE FOR 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. Find the number of students who have scored below 50 in their average.");
System.out.println("4. Display the scores obtained by every student.");
System.out.println("5. Exit program");
System.out.print("Enter the operation you want to perform: ");
int option = sc.nextInt();
//Lets populate our array with marks for 20 students in 5 subjects
//This will be used to test our program
int[][] MARKS = { {80, 98, 75, 58,95},
{52, 38, 64, 36,28},
{58, 56, 82, 92,46},
{98, 58, 68, 74,42},
{88, 38, 81, 54,46},
{76, 88, 72, 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},
{96, 38, 92, 54,52},
{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_sum = 0;
for(int j = 0; j< 20; j++)
{
subject_sum = subject_sum + MARKS[j][i];
}
System.out.println("The average mark for Subject "+(i+1)+" = "+(subject_sum/20));
}
}
else if(option==2)
{
System.out.println("In 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)
{
int count_for_number_avg_below_50 = 0;
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];
}
float average = student_total_marks/5;
if(average < 50)
{
count_for_number_avg_below_50 = count_for_number_avg_below_50 + 1;
}
}
System.out.println("The number of students with average below 50 = "+count_for_number_avg_below_50);
}
else if(option==4)
{
for(int i = 0; i<20;i++)
{
System.out.println("Student "+(i+1)+" scores:");
for(int j = 0; j<5; j++)
{
System.out.print("Subject "+(j+1)+" = "+MARKS[i][j]+", ");
}
System.out.println("");
}
}
else if(option==5)
{
System.out.println("Program exited successfully");
}
else{
System.out.println("Invalid option please try again");
}
}
}
Comments
Leave a comment