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
Similar post
Calculate the total of array elements in Java
Calculate the total of array elements in C
Calculate the total of array elements in C++
Java program to calculate sum of array elements
C program to calculate sum of array elements
Java Program to calculate average of array
C Program to calculate average of array
Java Program to calculate average of odd and even numbers in an array
Java Program to calculate average of odd and even numbers
C Program to calculate average of odd and even numbers in an array
C Program to calculate average of odd and even numbers
C++ Program to calculate average of odd and even numbers in an array
C++ Program to calculate average of odd and even numbers
Suggested for you
Single dimensional array in C language
Data type and variable in C language
Single dimensional array in C++ language
Single dimension array in Java
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…