The following code is to accept the details on 'n' number(s) of student(s) and display the total and the average of all. Maximum students are 60,000 and can be increased by increasing the array size.
Code:-
import java.util.*;
public class Marks{
Scanner scan=new Scanner(System.in);
String name[]=new String[60000];
String sub[]=new String[60000];
int roll[]=new int[60000];
int marks[]=new int[60000];
int total, students;
int i,e;
float average;
public void input(){
try{
System.out.println("Enter the total number of students: ");
students=scan.nextInt();
for(i=1; i<=students; i++){
for(e=1; e<=1; e++){
System.out.println("Enter "+i+" student name: ");
name[i]=scan.next();
}
for(e=1; e<=1; e++){
System.out.println("Enter "+name[i]+"'s Roll No: ");
roll[i]=scan.nextInt();
}
for(e=1; e<=1; e++){
System.out.println("Enter "+name[i]+"'s Subject name: ");
sub[i]=scan.next();
}
for(e=1; e<=1; e++){
System.out.println("Enter the marks "+name[i]+" obtained in "+sub[i]+": ");
marks[i]=scan.nextInt();
}
}
}
catch(Exception e){
System.out.println("Following Error occured while processing: "+e);
System.exit(0);
}
}
public void compute(){
for(i=1; i<=students; i++)
total+=marks[i];
average=total/students;
}
public void display(){
System.out.println("Details of "+students+" students are as follow:- ");
for(e=1; e<=students; e++){
for(i=1; i<=1; i++)
System.out.println(e+" student Name: "+name[e]);
for(i=1; i<=1; i++)
System.out.println(name[e]+"'s Roll No: "+roll[e]);
for(i=1; i<=1; i++)
System.out.println(name[e]+"'s Marks in "+sub[e]+" are: "+marks[e]+"\n");
}
System.out.println("Total Marks of "+students+" students are: "+total);
System.out.println("Average Marks of "+students+" students are: "+average);
}
public static void main(String[] args){
Marks obj=new Marks();
obj.input();
obj.compute();
obj.display();
}
}
Output:-
Enter the total number of students:
2
Enter 1 student name:
Jean
Enter Jean's Roll No:
05
Enter Jean's Subject name:
English
Enter the marks Jean obtained in English:
95
Enter 2 student name:
Dave
Enter Dave's Roll No:
15
Enter Dave's Subject name:
Maths
Enter the marks Dave obtained in Maths:
85
Details of 2 students are as follow:-
1 student Name: Jean
Jean's Roll No: 5
Jean's Marks in English are: 95
2 student Name: Dave
Dave's Roll No: 15
Dave's Marks in Maths are: 85
Total Marks of 2 students are: 180
Average Marks of 2 students are: 90.0
Code:-
import java.util.*;
public class Marks{
Scanner scan=new Scanner(System.in);
String name[]=new String[60000];
String sub[]=new String[60000];
int roll[]=new int[60000];
int marks[]=new int[60000];
int total, students;
int i,e;
float average;
public void input(){
try{
System.out.println("Enter the total number of students: ");
students=scan.nextInt();
for(i=1; i<=students; i++){
for(e=1; e<=1; e++){
System.out.println("Enter "+i+" student name: ");
name[i]=scan.next();
}
for(e=1; e<=1; e++){
System.out.println("Enter "+name[i]+"'s Roll No: ");
roll[i]=scan.nextInt();
}
for(e=1; e<=1; e++){
System.out.println("Enter "+name[i]+"'s Subject name: ");
sub[i]=scan.next();
}
for(e=1; e<=1; e++){
System.out.println("Enter the marks "+name[i]+" obtained in "+sub[i]+": ");
marks[i]=scan.nextInt();
}
}
}
catch(Exception e){
System.out.println("Following Error occured while processing: "+e);
System.exit(0);
}
}
public void compute(){
for(i=1; i<=students; i++)
total+=marks[i];
average=total/students;
}
public void display(){
System.out.println("Details of "+students+" students are as follow:- ");
for(e=1; e<=students; e++){
for(i=1; i<=1; i++)
System.out.println(e+" student Name: "+name[e]);
for(i=1; i<=1; i++)
System.out.println(name[e]+"'s Roll No: "+roll[e]);
for(i=1; i<=1; i++)
System.out.println(name[e]+"'s Marks in "+sub[e]+" are: "+marks[e]+"\n");
}
System.out.println("Total Marks of "+students+" students are: "+total);
System.out.println("Average Marks of "+students+" students are: "+average);
}
public static void main(String[] args){
Marks obj=new Marks();
obj.input();
obj.compute();
obj.display();
}
}
Output:-
Enter the total number of students:
2
Enter 1 student name:
Jean
Enter Jean's Roll No:
05
Enter Jean's Subject name:
English
Enter the marks Jean obtained in English:
95
Enter 2 student name:
Dave
Enter Dave's Roll No:
15
Enter Dave's Subject name:
Maths
Enter the marks Dave obtained in Maths:
85
Details of 2 students are as follow:-
1 student Name: Jean
Jean's Roll No: 5
Jean's Marks in English are: 95
2 student Name: Dave
Dave's Roll No: 15
Dave's Marks in Maths are: 85
Total Marks of 2 students are: 180
Average Marks of 2 students are: 90.0
Comments