Java program to find sum & average of an array

Let’s learn java program to find sum & average of an array.

Java program to find sum & average of an array

In java average of array program user enters elements. Next in for loop we calculate sum. Meanwhile divide by number of elements in array to calculate average.

Also read – comments in java

Here’s the java average of array and sum of an array,

import java.util.Scanner;

public class SumAverageArrayDemo 
{
   public static void main(String[] args) 
   { 
      int number, sum = 0;
      float average;
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter number of elements : ");
      number = sc.nextInt();
      int arrNum[] = new int[number];
      System.out.println("Enter " + number + " elements : ");
      for(int a = 0; a < number ; a++)
      {
         arrNum[a] = sc.nextInt();
         sum = sum + arrNum[a];
      }
      System.out.println("Sum is : " + sum);
      // average of an array
      average = (float)sum / number;
      System.out.println("Average is : " + average);
      sc.close();
   }
}



Output:

java program to find sum & average of an array

Java program to calculate average of 3 numbers

To find average of 3 numbers we are taking input from user and storing in three variables.

These variables are then passed to calculateAverage() method to find average.

import java.util.Scanner;

public class AverageNumberDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter first number: ");
      double number1 = sc.nextDouble();
      System.out.print("Please enter second number: ");
      double number2 = sc.nextDouble();
      System.out.print("Please enter third number: ");
      double number3 = sc.nextDouble();
      System.out.print("The average of 3 numbers is: " + calculateAverage(number1, number2, number3));
      sc.close();
   }

   public static double calculateAverage(double num1, double num2, double num3)
   {
      return (num1 + num2 + num3) / 3;
   }
}


Output:

Please enter first number: 226
Please enter second number: 256
Please enter third number: 655
The average of 3 numbers is: 379.0

java program to find average of n numbers

To find average of n numbers create variable num and set count equal to 1. Next create variables ‘a’, average and total = 0 of type float.

In the while loop check count is less than or equal to num. Then total += a and count = count + 1.

Now set average = total / num. Lastly print average entered by user.

import java.util.Scanner;

public class FindAverage 
{
   public static void main(String[] args) 
   {
      int num, count = 1;   
      float a, average, total = 0;   
      Scanner sc = new Scanner(System.in);     
      System.out.println("Please enter n numbers: ");  
      num = sc.nextInt();  
      while(count <= num)   
      {   
         System.out.println(count + " number: ");  
         a = sc.nextInt();  
         total += a;   
         ++count;   
      }
      average = total / num;   
      System.out.println("Average is: " + average);
      sc.close();
   }
}


Output:

Please enter n numbers: 6
1 number: 6
2 number: 9
3 number: 9
4 number: 9
5 number: 5
6 number: 9
Average is: 7.8333335

java program to calculate average marks

Here’s the java program to calculate average and percentage of marks,

import java.util.Scanner;

public class AverageMarksExample 
{
   public static void main(String[] args) 
   {
      int[] marks = new int[6];
      float total = 0;
      float average, percentage;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter marks of 6 subjects: ");
      for(int a = 0; a < 6; a++)
      {
         marks[a] = sc.nextInt();
         total = total + marks[a];
      }
      average = total / 6;
      percentage = (total / 600) * 100;
      System.out.print("Average marks = " + average);
      System.out.print("\nPercentage is = " + percentage + "%");
      sc.close();
   }
}


Output:

Please enter marks of 6 subjects: 91
92
96
90
98
94
Average marks = 93.5
Percentage is = 93.5%

This is all about java program to find sum & average of an array. I have shown all examples with repect to sum and average of an array in java.

Above discussed java programs will be useful for freshers who are learning java programs for interview.