Finding Mean Value Of An Integer Array In Java

In Basic Algebra, mean is nothing but just an average of all the points and is calculated as a sum of all the numbers divided by the total count. Don’t be surprised if you are asked to write a java program that can calculate mean value of an array of integers.

The interviewer might be trying to test your coding style, clean coding ability and ability to think about the boundary conditions and user errors. As you can see in the program below, I have written the method to be fail proof from null value or an empty integer.


import java.util.Arrays;

 * @author Kushal Paudyal
 * Created On -  Feb 21, 2014
 * Last Modified On - Feb 21, 2014
 * This class can be used to find the mean value in an array
public class AlgebraMeanFinder {
	public static void main(String args []) {
		int [] numbers = {5,5,7,2,3,9,8,9,2,6,7,8,1};
		 * Finding mean number of a valid integer array
		 * Testing for null array
		 * Passing an empty array
		findMeanValue(new int[] {});

	 * @param numbers - array of numbers whose mean has to be found
	 * Mean is a simple average value of all numbers.
	 * This method is capable of handling boundary conditions.
	public static double findMeanValue(int[] numbers) {

		double sum = 0;
		if (numbers == null || numbers.length < 1) {
			System.out.println("\nInvalid Input. Returning 0. Input was:" + Arrays.toString(numbers));
			return sum;

		for (int index = 0; index < numbers.length; index++) {
			sum += numbers[index];

		double mean = (sum / numbers.length);
		System.out.println("\nInput Array is: " + Arrays.toString(numbers));
		System.out.println("Sum:" + sum + " Count:" + numbers.length + " Mean Value is: " + mean);

		return mean;



Here is the output of this program.

Input Array is: [5, 5, 7, 2, 3, 9, 8, 9, 2, 6, 7, 8, 1]
Sum:72.0 Count:13 Mean Value is: 5.538461538461538

Invalid Input. Returning 0. Input was:null

Invalid Input. Returning 0. Input was:[]
  1. Recursively Finding Greatest Common Divisor (GCD) – Java Implementation
  2. Implementing a Simple LIFO Stack in Java using LinkedList
  3. Implementing a Simple FIFO Queue in Java Using Linked List
  4. Protected: Behavioral Questions – Interview
  5. In Place Matrix (2D Array) Clockwise and Counterclockwise Rotation – Java Implementation
  6. Matrix (2D Array) Clockwise and Counterclockwise Rotation with Extra Buffer – Java Implementation
  7. Array Max Integer Finder (With Big O Analysis)
  8. A Basic Implementation of Binary Tree in Java
  9. A Basic Implementation of Generic Tree in Java
  10. Basic example of implementing Singly Linked List in Java
  11. Top XML Interview Questions
  12. J2EE Interview Questions
  13. Prime Number Finder In Java
  14. Webservice Interview Questions
  15. Core Java Interview Preparation Questions
  16. Printing Fibonacci Sequence Using Recursive and Iterative Methods
  17. Finding Square Root Of A Double Number In Java Using Binary Search
  18. How to Add and Remove nodes in an unsorted Linked List
  19. How to swap two variables without using extra temporary variable?
  20. Rotating a two dimensional integer array In-Place and using extra memory
  21. How to reverse a Singly Linked List iteratively and recursively
  22. Sorted Circular Linked List Implementation And Insert Operation
  23. Finding Mean Value Of An Integer Array In Java
  24. How to intersect two sets in Java using java.util.Set data structure.
  25. Bubble Sorting An Integer Array In Ascending and Descending Order
  26. How to split strings and separate the words with spaces
  27. Finding if two Strings are Anagrams
  28. How to easily sort characters in a String?
Tagged , , , . Bookmark the permalink.

Leave a Reply

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