Java Online Compiler
Example: Average of N Numbers in Java using Array
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Average of N Numbers in Java using Array import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println ("Enter the number of elements to calculate the average::"); int x=in.nextInt(); // Counting of input elements float avg=0; // Average of the input elements System.out.println ("\nEnter " + x + " elements one by one"); int arr[] = new int[x]; // Array initialized with length // It will read the inputs and puts them into an array variable // It will make a sum of each element of the array for(int i=0; i<x; i++) { arr[i]=in.nextInt(); avg += arr[i]; } avg/=x; // It will print the final output System.out.println ("\nThe average of the entered input numbers is = " + avg); } }
4 34562 35223 65534 45685
Output
Clear
ADVERTISEMENTS