Java Program Calculate Sum And Average Of Array

Sharing is healthy !!

In the following post you will learn java program calculate sum and average of array.

Here i am going to show you how to execute java program using Scanner class and without using Scanner class. So, let us get into code implementation,

Example: Java Program Calculate Sum And Average Of Array

Also Read – Binary Search Program Java

public class Average_Array 
{
public static void main(String[] args) 
{
int num[] = new int[]{30,12,14,24,19,89,128};
int total = 0;

for(int a = 0;a < num.length;a++)
{
   total = total + num[a];
}

double average = total / num.length;

System.out.println("The total is : " + total);
System.out.println("Average value of an array is: " + average);
}
}

Output:

The total is : 316
Average value of an array is: 45.0


using scanner class

Also Read – Bitwise Addition Program

import java.util.Scanner;

public class Average_Array
{
public static void main(String[] args) 
{
int a;
int total = 0;
double average;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of integers in an array: ");
a = sc.nextInt();
int num[] = new int[a];
System.out.println("Enter " + a + " integers.");

for(int i = 0;i < a;i++)
{
   num[i] = sc.nextInt();
   total = total + num[i];
}
System.out.println("Total: " + total);
average = (double)total / a;
System.out.println("Average value of an array is: " + average);
}
}

Output:

Java Program Calculate Sum And Average Of Array


conclusion

That’s it guys. This all about java program calculate sum and average of array.

You have learnt above program with scanner class and without scanner class. I hope you enjoyed this post.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Sharing is healthy !!

Leave a Reply

Your email address will not be published.