# Sum and Average the elements of an array for Java Example

by Dinesh Thakur Category: array

Algorithm for Sum and Average the Elements:

step 1: set sum=0

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 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.in);

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++)

{

arr[i]=s.nextInt();

sum+=arr[i];

}

float avg=(float)(sum/arr.length);

System.out.println("Sum is "+sum+" Average is "+avg);

}

}

```  Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles