Java Program to Find Average of Numbers In Array

By Saruque Ahamed Mollick

This below Java Program will find the average of integer elements from Array.

Java Code To Find the Average value of Number Elements ( Elements will be taken from user inputs) from an array

import java.util.Scanner;

public class Codespeedy {
    public static void main (String[] args) {  
        Scanner input=new Scanner(System.in);
        System.out.println("Enter the size of array:");
        int n=input.nextInt();
        double total=0;
        double array[]=new double[n]; 
        System.out.println("Enter the elements one by one:");
        for (int s=0;s<array.length;s++){
            array[s]=input.nextDouble();
        }
        for(int i=0;i<array.length;i++){
            total=total+array[i];
        }
        double average=total/n;
        System.out.println("The averag value is:"+average);
        
    }  
}

Output:

 

run:
Enter the size of array:
5
Enter the elements one by one:
22.11
55.22
54.28
48.658
15.25
The averag value is:39.10360000000001
BUILD SUCCESSFUL (total time: 16 seconds)

Merge (Combine) Two or More Arrays Into A Single Array In Java



Algorithm and Explanation Of This Java Average Value Calculator Program

  • Take the size of the array from user input.
  • Take the elements one by one from the user using for loop and array index.
  • Add each element to the previous element and store it in a variable. (I have initialized the variable total with zero used it)
  • After adding all the variables, divide the total value with the number of elements in the array. (total/n)
  • Store the result in any variable.
  • Print the final result to show the average value to the user.

Remove Duplicate Elements From Unsorted Array And Print Sorted

for (int s=0;s<array.length;s++){
            array[s]=input.nextDouble();
        }

This is the array input part from the user.

for(int i=0;i<array.length;i++){
            total=total+array[i];
        }

This is the main algorithm of our program to add the elements of our array

double average=total/n;
        System.out.println("The averag value is:"+average);

This is for output statement.

A Java Program To Find Duplicate Values In an Array

Leave a Reply

Your email address will not be published. Required fields are marked *