Java Online Compiler
Example: Average of N Numbers in Java using While loop
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Average of N Numbers in Java using While loop 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 y=0, i=0; while (i < x) { y=in.nextInt(); avg += y; i++; } avg /= x; System.out.println ("\nThe average of the entered input numbers is = " + avg); } }
3 34563 3522 6553
Output
Clear
ADVERTISEMENTS