Skip to main content

Java Program to accept the marks of n students' one subject and display the total and average

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

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

import java.io.*; import java.util.*; public class StrikeRate{     Scanner scan=new Scanner(System.in);     int ballsFaced, runs;     double strikeRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Faced: ");             ballsFaced=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0); ...

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...