Codeforcoding

Java Program to calculate average of an Array

Java Program to calculate the average of an Array

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

Java Program to find average of an Array
Calculate average

find the average of integer values

Program 1

In this program, we can see step by step procedure for completion of the program.

Calculate the average of numbers using an array

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.

Calculate the average of numbers using an array – entered by the user

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

 

find the average of floating point values

Program 3

In this program, we can see what should be step by step procedure for completion of the program.

Calculate the average of numbers using an array

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.

Calculate the average of numbers using an array – entered by the user

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

Operator in C language

for loop in C language

Single dimensional array in C language

Data type and variable in C language

Operator in C++ language

For loop in C++ language

Single dimensional array in C++ language

The data type in C++ language

Operator in Java

for loop in Java

Single dimension array in Java

 

C program to multiply two numbers using function
Cpp program to calculate sum of array elements
Exit mobile version