Calculations

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

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.

  • array declaration and initialization
  • essential variable declaration
  • loop for calculating total through each value
  • Calculating average
  • Display result on the screen

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.

  • Array declaration
  • essential variable declaration
  • loop for taking input from the user for every index
  • loop for calculating total through each value
  • Calculating average
  • Display result on the screen

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.

  • array declaration and initialization
  • essential variable declaration
  • loop for calculating total through each value
  • Calculating average
  • Display result on the screen

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.

  • Array declaration
  • Essential variable declaration
  • loop for taking input from the user for every index
  • loop for calculating total through each value
  • Calculating average
  • Display result on the screen

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
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago