Java Program to Calculate average using Array

Published: 03 June 2021
on channel: Aman Tripathi
65
7

import java.util.Scanner;
public class JavaExample {

public static void main(String[] args) {
System.out.println("How many numbers you want to enter?");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
/* Declaring array of n elements, the value
of n is provided by the user
*/
double[] arr = new double[n];
double total = 0;

for(int i=0; i_arr.length; i++){
System.out.print("Enter Element No."+(i+1)+": ");
arr[i] = scanner.nextDouble();
}
scanner.close();
for(int i=0; i_arr.length; i++){
total = total + arr[i];
}



double average = total / arr.length;

System.out.format("The average is: %.3f", average);
}
}
Output:

How many numbers you want to enter?
5
Enter Element No.1: 12.7
Enter Element No.2: 18.9
Enter Element No.3: 20
Enter Element No.4: 13.923
Enter Element No.5: 15.6
The average is: 16.225


On this page of the site you can watch the video online Java Program to Calculate average using Array with a duration of hours minute second in good quality, which was uploaded by the user Aman Tripathi 03 June 2021, share the link with friends and acquaintances, this video has already been watched 65 times on youtube and it was liked by 7 viewers. Enjoy your viewing!