Menu Close

Calculate the average value of array element

TEST CASE 1
INPUT
N = 4
ARR = 34 89 100 78

OUTPUT
75

TEST CASE 2
INPUT
N= 5
ARR = 5 4 3 2 1

OUTPUT
3

import java.io.*;
import java.util.Scanner;
public class temp{
  public static void main(String[] args){
    int n, sum = 0;
    int average;
    Scanner s = new Scanner(System.in);
    System.out.printf("Enter n Number: ");
    n = s.nextInt();
    int a[] = new int[n];
    System.out.printf("Enter Array Numbers: ");
    for(int i = 0; i < n ; i++){
      a[i] = s.nextInt();
      sum = sum + a[i];
    }
    average = (int)sum / n;
    System.out.printf("Average = %.2f \n",(float)average);
  }
}

OUTPUT:

Input:
Enter n Number: 5
Enter Array Numbers: 66 55 88 99 55
Output:
Average = 72.00

Input:
Enter n Number: 3
Enter Array Numbers: 1 2 3
Output:
Average = 2.00

Input:
Enter n Number: 4
Enter Array Numbers: 2 4 68 8
Output:
Average = 20.00

Input:
Enter n Number: 4
Enter Array Numbers: 22 22 22 22
Output:
Average = 22.00

Input:
Enter n Number: 3
Enter Array Numbers: 33 33 33
Output:
Average = 33.00

Screenshot