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

 

Suggested for you

Operator in Java

for loop in Java

Single dimension array in Java

Calculate the total of array elements 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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago