Sum And Average Of Values In An Array In Java

Hello guys!! Welcome to flower brackets blog. In this post we going to learn how to calculate sum and average of values in an array in java.

Okay, in this program we will use an array and loop to calculate the sum and average of a set of values in java.

So let’s start by java average of array,

Example : array sum java

Let’s see an example on how to sum arrays and average in java,

public class SumAverageDemo
{
   public static void main(String[] args)
   {
      double var[] = {2,4,6,8,10};
      double sum = 0;
      for(double value : var)
      {
         sum += value;
      }
      System.out.println("The sum is : " + sum);
      double average = 0;
      if(var.length > 0)
      {
         average = sum / var.length;
      }
      System.out.println("The average is : " + average);
   }
}

Output:

Sum And Average Of Values In An Array In Java


Array sum java – maximum and minimum values

Right, this program will show you how to calculate the maximum and minimum values inside of an array.

Also Read – Factorial Java

So I just have array here,

Maximum:

public class SumAverageDemo
{
   public static void main(String[] args)
   {
      double var[] = {21,40,61,85,100};
      double max = var[0];
      for(int a = 0; a < var.length; a++)
      {
         if(var[a] > max)
         {
            max = var[a];
         }
      }
      System.out.println("The maximum number is : " + max);
   }
}

Output:

The maximum number is : 100.0


Minimum :

public class SumAverageDemo
{
   public static void main(String[] args)
   {
      double var[] = {21,40,61,85,100};
      double min = var[0];
      for(int a = 0; a < var.length; a++)
      {
         if(var[a] < min)
         {
            min = var[a];
         }
      }
      System.out.println("The minimum number is : " + min);
   }
}

Output:

The minimum number is : 21.0


Array sum java – using for loop

public class JavaSum 
{
   public static void main(String[] args) 
   {
      int numbers[] = {5, 10, 15, 20, 25};
      int sum = 0;
      for(int num : numbers)
      {
         sum = sum + num;
         System.out.println(num);
      }
      System.out.println("Sum of an array : " + sum);
   }
}

Output :

5
10
15
20
25
Sum of an array : 75


Array sum and average in java – using java stream api

Let’s see an example on how to sum an array in java using java stream api,

import java.util.Arrays;

public class SumArrays 
{
   public static void main(String[] args) 
   {
      int num[] = { 5, 10, 15, 20, 25 }; 

      // using for loop which we are familiar with 
      int sum = 0; 
      for(int a = 0; a < num.length; a++) 
         sum += num[a]; 
      System.out.println("Average of array java using iteration : " + (sum / num.length)); 

      // using java stream API 
      sum = Arrays.stream(num).sum(); 
      System.out.println("Sum of an array using streams : " + sum); 

      // forEach() iterates through the entire streams 
      System.out.println("Printing array of integers java : "); 
      Arrays.stream(num).forEach(e -> System.out.print(e + " ")); 
   }
}

Output :

Average of array java using iteration : 15
Sum of an array using streams : 75
Printing array of integers java :
5 10 15 20 25


conclusion:

So this is all about how to calculate sum and average of values in an array. We also learnt how to calculate maximum and minimum values in an array.

I hope you had fun and have understood calculate average in java. Subscribe to my blog flower brackets if you haven’t.

If you find this post useful share it across social networks.

Related Posts