This is a Java Program to Calculate Sum & Average of an Array.
Enter size of array and then enter all the elements of that array. Now using for loop we calculate sum of elements of array and hence we divide it by number of elements in array to get average.
Here is the source code of the Java Program to Calculate Sum & Average of an Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Sum_Average
public static void main(String args)
int n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n ; i++)
a[i] = s.nextInt();
sum = sum + a[i];
average = (float)sum / n;
$ javac Sum_Average.java $ java Sum_Average Enter no. of elements you want in array:5 Enter all the elements: 4 7 6 9 3 Sum:29 Average:5.8
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Information Technology Internship
- Buy Java Books
- Practice Programming MCQs
- Practice BCA MCQs
- Apply for Java Internship