Java Program to find Sum, Product, and Average of an Array

Ā 

Write a Java Program to find Sum, Product, and Average of an Array

Write a Java Program to Find the Sum of array elements, Product of array elements, and Average of elements of a single dimensional array.

Video Tutorial:

Steps (Program logic):

1. Declare the local variables like a, sum, prod, avg to store array elements, the sum of the array, a product of array, and an average of array elements respectively.

2. Read the number of elements in an array.

3. Instantiate the array with the number of array elements.

4. Read the elements one by one and store in array.

5. Find the sum, product of elements of an array, then find the average using the sum and number of elements in an array.

6. Finally, display the sum, product and average of elements of an array.

Source code of Java Program to find Sum, Product, and Avg. of an Array

import java.util.Scanner;
 /*
 * Java Program to find the Sum, Product and Average of elements single  
 * dimensional array

 * Subscribe to Mahesh Huddar YouTube Channel for more Videos
 */ 
 public class ArraySumProductAvg 
 {
     public static void main(String args[]) 
     {
         int a[], sum = 0, prod = 1, avg, num;
         Scanner in = new Scanner(System.in);
         System.out.println("Enter the number of array elements:");
         num = in.nextInt();
         a = new int[num];
         System.out.println("Enter the array elements: ");
         for (int i = 0; i < num; i++)
         {
             System.out.println("Enter the "+(i+1)+" element:");
             a[i] = in.nextInt();
         }
         for (int i = 0; i < num; i++)
         {
             sum = sum + a[i];
             prod = prod * a[i];
         }
         avg = sum / num;
         System.out.println("Sum of array elements is: "+sum);
         System.out.println("Product of array elements is: "+prod);
         System.out.println("Average of array elements is: "+avg);
     }
 }

Output:

Enter the number of array elements: 5

Enter the array elements:

Enter the 1 element: 5

Enter the 2 element: 10

Enter the 3 element: 15

Enter the 4 element: 20

Enter the 5 element: 25

Sum of array elements is: 75

Product of array elements is: 375000

Average of array elements is: 15

If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds