Java Program to Calculate and Print Average of the Stream of Given Numbers

In the previous article we have discussed about Java Program to Calculate Power of a Number

In this program we are going to see how to calculate and print average of the stream of given numbers by using Java Programming language.

Java Program to Calculate and Print Average of the Stream of Given Numbers

Before jumping into the program, let’s know the formula to find average of given numbers.

Average= (sum of all item’s values) / (Total number of items)

Example:

Suppose a=20, B=30, Then

Average=(sum of value a and b)/2
= (20+30)/2
= 25

Now let’s see different ways to calculate and print average of the stream of given numbers.

Method-1: Java Program to Calculate and Print Average of the Stream of Given Numbers By Using Static Input Value

Approach:

  • Declare an array ‘arr‘ of float type and assign value to it.
  • Declare another integer variable ‘n‘ to store the length of the array.
  • Declare a float variable ‘avg‘ to store the result.
  • By using for loop print the required result continuously.
  • Use the formula inside for loop to calculate the result
  • Print the result.

Program:

class Main 
{     
    public static void main(String[] args) 
    { 
        //Declare an array arr of float type and assign array elements to it. 
        float arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; 
        //declare another integer variable n to store the length of the array
        int n = arr.length; 
        //Declare a float variable avg to store the result.
        float avg = 0; 
        //performing for loop to print the required result continuously
        for (int i = 0; i < n; i++)  
        { 
            //use the formula to calculate the result
            avg = (avg * i + arr[i]) / (i + 1);
            //print the result
            System.out.printf("Average of %d numbers is %f \n",i + 1, avg); 
        } 
    }
}
Output:

Average of 1 numbers is 10.000000 
Average of 2 numbers is 15.000000 
Average of 3 numbers is 20.000000 
Average of 4 numbers is 25.000000 
Average of 5 numbers is 30.000000 
Average of 6 numbers is 35.000000 
Average of 7 numbers is 40.000000 
Average of 8 numbers is 45.000000 
Average of 9 numbers is 50.000000 
Average of 10 numbers is 55.000000

Method-2: Java Program to Calculate and Print Average of the Stream of Given Numbers By Using User Input Value

Approach:

  • Declare another integer variable ‘n‘ to store the length of the array.
  • Declare an array arr of float type with an array-size of n
  • By using a for loop take input of the elements for array.
  • Declare a float variable ‘avg‘ to store the result.
  • By using for loop print the required result continuously.
  • Use the formula inside for loop to calculate the result
  • Print the result.

Program:

import java.util.Scanner;
class Main 
{     
    public static void main(String[] args) 
    { 
        //create the object of the scanner class
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the number of element of array: ");
        //declare an integer variable n to store the length of the array
        int n = sc.nextInt();
        //declare an array arr of float type with an array-size of n
        float arr[]= new float[n];
        System.out.println("Enter the elements of array: ");
        //performing a for loop to continuously take the element for array
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextFloat();
        }
         
        //Declare a float variable avg to store the result.
        float avg = 0; 
        //performing for loop to print the required result continuosly
        for (int i = 0; i < n; i++)  
        { 
            //use the formula to calculate the result
            avg = (avg * i + arr[i]) / (i + 1);
            //print the result
           	System.out.printf("Average of %d numbers is %f \n",i + 1, avg); 
        } 
    }
}
Output:

Enter the number of element of array: 3
Enter the elements of array: 
12
23
34
Average of 1 numbers is 12.000000 
Average of 2 numbers is 17.500000 
Average of 3 numbers is 23.000000

Method-3: Java Program to Calculate and Print Average of the Stream of Given Numbers By Using User Defined Method

Approach:

  • Declare another integer variable ‘n‘ to store the length of the array.
  • Declare an array arr of float type with an array-size of n
  • By using a for loop take input of the elements for array.
  • Then call a user defined method say computeValue() and pass arr[] as parameter.
  • Then inside method by using the formula compute avg
  • Print the result.

Program:

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        //create the object of the scanner class
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the number of element of array: ");
        //declare an integer variable n to store the length of the array
        int n = sc.nextInt();
        //declare an array arr of float type with an array-size of n
        float arr[]= new float[n];
        System.out.println("Enter the elements of array: ");
        //performing a for loop to continuously take the element for array
        for(int i = 0; i < arr.length; i++)
        {
            arr[i]=sc.nextFloat();
        }
        //call the funtion
        computeValue(arr);
    }
    
    //define the method
    public static void computeValue(float arr[])
    {
        //Declare a float variable avg to store the result.
        float avg = 0; 
        //performing for loop to print the required result continuosly
        for (int i = 0; i < arr.length; i++)  
        { 
            //use the formula to calculate the result
            avg = (avg * i + arr[i]) / (i + 1);
            //print the result
            System.out.printf("Average of %d numbers is %f \n",i + 1, avg); 
        } 
    }
}
Output:

Enter the number of element of array: 5
Enter the elements of array: 
10
39
45
67
32
Average of 1 numbers is 10.000000 
Average of 2 numbers is 24.500000 
Average of 3 numbers is 31.333334 
Average of 4 numbers is 40.250000 
Average of 5 numbers is 38.599998

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: