pTutorial

New way to learn Programming

Java array average example

Following Java program calculate the average marks (average) of entered subject marks. It’s completely user defined program to enter number of subject and subject marks.

Scanner class makes your program user define that is not part of the java.lang package so you will have to import this class.

Java print average marks

// java program to print avarage marks

import java.util.Scanner;
public class StudentMarks {
    static int n;
    public static void input1(int[] b)
    {
        Scanner uk=new Scanner(System.in);
        System.out.println("Enter " + n + " subject marks");
        for(int l=0;l<b.length;l++){
            b[l]= uk.nextInt();
        }
    }
    
    public static void avg(int[] p){
        int sum=0;
        for(int l=0;l<p.length;l++){
            sum=sum+p[l];
        }
        sum=sum/n;
        System.out.println("Averege marks:"+sum);
    }

    public static void main(String[] args) {
        Scanner uk=new Scanner(System.in);
        System.out.println("Enter the number of Subject ");
        n=uk.nextInt();
        int x[]=new int[n];	
        input1(x);
        avg(x);
    }
}



Output

Enter the number of Subject 
3
Enter 3 subject marks
120
123
123
Averege marks:122

See Also


QUOTES OF THE DAY

The heart of melody can never be put down.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER