In this tutorial, we are going to learn a writing java program to find the average of numbers given as input from the user. This program is little bit different from the previous Average program, Where we have calculated the average of Integer array in java.

Page Contents

## Problem Statement

For any sequence of input numbers that are given by the user, we have to calculate the average of the input numbers using java program.

**For example**

**Case1:** If the Input sequence given by user are 1,2,3,4,5.

The output should be 3.**Case2:** If the Input Sequence given by user are 11,22,33,44,55,66.

The output should be 38.5.

## What is the average of the numbers?

Average also called the arithmetic mean, is calculated by adding a group or collection of numbers and then dividing that sum by the number of elements of the same group or collection.

## Algorithm to calculate average of the numbers

**Step 1:** Start

**Step 2:** float sum =0;

**Step 3: **Scanner sc = new Scanner(System.in);

**Step 4:** input array size int n= sc.nextInt();

**Step 5:** create an empty array of size n.

**Step 6:** for(int i=0; i < n; i++) { arr[i] = sc.nextInt(); sum=sum+arr[i]; }

**Step 7:** assign a variable, avg as float average = sum/n;

**Step 8:** print average

**Step 9:** Stop

## Our Logic to calculate the Average of Number Given by users

- Our program will have declare the variable sum =0.
- Also our program will take an integer input from user as the size of array.
- Then In the empty array, elements of the array will be taken from the user using ‘For’ loop.
- Then. Our program will calculate the sum of all the numbers using ‘for loop’.
- Finally, the calculated sum is then divided by the size of array and print the result as the output.
- And Our final output will be the average of the number given by the user.

## Java Program to calculate Average of Given Input Integer

**Output**

**Explanation**

In this example, the input array is {1, 2, 3, 4, 6}. Where the size of array is 5 so the average will be

Average = (1+2+3+4+6)/ 5 , The output generated is 3.2.