In this tutorial, we discuss a simple concept of Java Program to calculate the average of an Array
In this topic, we can learn how to calculate the average of n number of elements (integer or floating point)using the array in Java programming language.
Average calculated using the operator in Java programming language
Program 1
In this program, we can see step by step procedure for completion of the program.
class Array_Avg{ public static void main (String args[]){ int[] arr={45,67,89,76,54,49,98,80};//declared and initialized an array int total=0;//declared and initialized total value for(int i=0; i<=arr.length-1; i++){//i<=arr.length-1 equalto i<arr.length total=total + arr[i]; } //arr.length provide the number of array element for calculate total double avg=total/arr.length; // calculate average of array elements System.out.println("The average of numbers are: "+avg); } }
When the above code is compiled and executed, it produces the following results
The average of numbers are: 69.0
Program 2
In this program, we can see step by step procedure for completion of the program.
import java.util.Scanner; class Array_Avg1{ public static void main (String args[]){ System.out.println("How many number you want to calculate average: "); Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int[] arr=new int[n];//declaring an array of n elements int total=0;//declared and initialized total value as zero //fist for loop for get input from user for(int i=0; i<=arr.length-1; i++){//i<=arr.length-1 equalto i<arr.length System.out.print("Enter the numbers "+(i+1)+": ");//n elements arr[i]=scan.nextInt(); } for(int i=0; i<=arr.length-1; i++){//i<=arr.length-1 equalto i<arr.length total=total + arr[i]; } //arr.length provide the number of array element for calculate total double avg=total/arr.length; // calculate average of array elements System.out.println("\nThe average of numbers are: "+avg); }//display result on the screen }
When the above code is compiled and executed, it produces the following results
How many numbers you want to calculate average: 6 Enter the number 1: 56 Enter the number 1: 79 Enter the number 1: 98 Enter the number 1: 80 Enter the number 1: 83 Enter the number 1: 76 The Average of numbers are: 78.0
Program 3
In this program, we can see what should be step by step procedure for completion of the program.
class Array_Avg2{ public static void main (String args[]){ double[] arr={68.56,35.90,69.76,60.12,43.54,40.34,80.3};//declared and initialized an array double total=0;//declared and initialized total value for(int i=0; i<=arr.length-1; i++){//i<=arr.length-1 equalto i<arr.length total=total + arr[i]; } //arr.length provide the number of array element for calculate total double avg=total/arr.length; // calculate average of array elements System.out.format("\nThe average is %.3f",avg); }//display the average on the screen }
When the above code is compiled and executed, it produces the following results
The average is 56.931
Program 4
In this program, we can see what should be step by step procedure for completion of the program.
import java.util.Scanner; class Array_Avg3{ public static void main (String args[]){ System.out.println("How many number you want to calculate average: "); Scanner scan=new Scanner(System.in); int n=scan.nextInt(); double[] arr=new double[n];//declaring an array of n elements double total=0;//declared and initialized total value as zero //fist for loop for get input from user for(int i=0; i<=arr.length-1; i++){//i<=arr.length-1 equalto i<arr.length System.out.print("Enter the numbers "+(i+1)+" :");//n elements arr[i]=scan.nextDouble(); } scan.close(); for(int i=0; i<=arr.length-1; i++){//i<=arr.length-1 equalto i<arr.length total=total + arr[i]; } //arr.length provide the number of array element for calculate total double avg=total/arr.length; // calculate average of array elements System.out.format("\nThe average is %.3f",avg); }//display the average on the screen }
When the above code is compiled and executed, it produces the following results
How many numbers you want to calculate average: 5 Enter the number 1: 45.8 Enter the number 1: 56.98 Enter the number 1: 76.43 Enter the number 1: 87.24 Enter the number 1: 57.43 The Average of numbers are: 64.776
Suggested for you
Single dimension array in Java
Calculate the total of array elements in Java
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…