by Dinesh Thakur Category: array

Algorithm for Sum and Average the Elements:

step 1: set sum=0

step 2: read arr_size

step 3: create an integer array arr[] of size arr_size

step 4: initialize i=0

step 5: repeat through step-11 while i less than arr.length

step 6: read arr[i]

step 7: sum=sum +arr[i]

step 8: i=i+ 1

step 9: set avg=(float)(sum/ arr.length)

step 10: print avg

step 11: Exit

Here is the Java Example for Sum and Average the Elements:

import java.util.Scanner;

public class ArraySumandAvg
              public static void main(String args[])
              int sum=0;
              Scanner s=new Scanner(;
              System.out.print("Enter The Size of the Array : ");
              int size=s.nextInt();
              int arr[]=new int[size];
              System.out.println("Enter the elements of Array");
              for(int i=0;i<arr.length;i++)
                    float avg=(float)(sum/arr.length);
            System.out.println("Sum is "+sum+" Average is "+avg);

Sum and Average the elements of an array

Popular Article