# Find the Average of Numbers in Java

Finding the mean or average of many numbers is one of the basic programs in Java learning. This is a simple java console program to find the mean of a set of numbers. Using this program, we can find the mean of unlimited numbers. At first, this program will accept the total number of items to. After this, program will accept all numbers from user separately.

You can also find the mean of many numbers without asking total number of items using array. Here we have to declare an array with a limit like 10. Like int nums[] = new int[10]; here this program accept only 10 number to find mean. After accepting all numbers, then program will find the mean using following mathematical formula.

Mean = Sum of total numbers of elements / number of elements.

Example: If number are 1,2,3.

Here sum is = 1+2+3 = 6

Here number of elements are 3

So that Mean is = 6 / 3 = 2

Here class name is ‘mean’, so you should save your file with name ‘mean.java’.

[Lean here how to create, compile and run a java application program here.]

__Program code:__import java.io.*; class mean { int num; double sum = 0; double result; void findmean() throws IOException { BufferedReader Br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\n Mean of how many numbers : "); num = Integer.parseInt(Br.readLine()); int nums[] = new int[num+1]; for(int i = 1;i<=num;i++) { System.out.print("\n Enter " + i + " number : "); nums[i]= Integer.parseInt(Br.readLine()); sum = sum + nums[i]; } result = sum / num; System.out.print("\n Mean of above numbers is : " + result ); } public static void main(String s[]) throws IOException { mean mn = new mean(); mn.findmean(); } } |

__Sample output:__Mean of how many numbers: 5

Enter 1 st Number : 1

Enter 2 nd Number : 2

Enter 3 rd Number : 3

Enter 4 th Number : 4

Enter 5 th Number : 5

Mean of above number is : 3.0

Like this, you can accept any numbers of items..

## No comments:

## Post a Comment