Menu Close

Average value of array element [Java] …ftc

Write a Java Program to Calculate the average value of array element.
Refer the testcases

Input:
5
5 4 3 2 1
Output:
3
import java.io.*;
import java.util.*;
public class temp{
  public static void main(String[] args)
  {
    int n, sum = 0;
    int average;
    Scanner s = new Scanner(System.in);
    System.out.print("Enter n number:  ");
    n = s.nextInt();
    int a[] = new int[n];

    System.out.print("Enter n arrays:  ");
    for(int i = 0; i < n ; i++)
    {
      a[i] = s.nextInt();
      sum = sum + a[i];
    }

    average = (int)sum / n;
    System.out.println("Average is "+average);
  }
}

INPUT_1:
Enter n number:  4
Enter n arrays:  34  89  100  78

OUTPUT:
Average is  75


INPUT_2:
Enter n number:  5
Enter n arrays:  5  4  3  2  1

OUTPUT:
Average is  3


INPUT_3:
Enter n number:  6
Enter n arrays:  1  2  3  4  5  6

OUTPUT:
Average is  3


INPUT_4:
Enter n number:  5
Enter n arrays:  100  250  326  541  589

OUTPUT:
Average is  361


INPUT_5:
Enter n number:  4
Enter n arrays:  44  55  88  11

OUTPUT:
Average is  49


INPUT_6:
Enter n number:  5
Enter n arrays:  01  10  11  110  111

OUTPUT:
Average is  48


INPUT_7:
Enter n number:  6
Enter n arrays:  56  65  101  10  45  54

OUTPUT:
Average is  55


ILLUSTRATION

EXECUTED USING JAVAC ON LINUX TERMINAL

Leave a Reply

Your email address will not be published. Required fields are marked *