Java Program to Find the Sum of All the Elements of an Array

In the previous article, we have seen Java Program to Replace Each Element of the Array with Sum of All Other Elements of the Array

In this article we are going to see how we can find the sum of elements of the array.

Java Program to Find the Sum of All the Elements of an Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the sum of elements of the array.

Method-1: Java Program to Find the Sum of All the Elements of an Array By Static Initialization of Array Elements

Approach:

  • Take an array with elements in it.
  • Print the array elements.
  • Find the sum of all elements by iterating using a for loop.
  • Print the sum.

Program:

import java.util.Arrays;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12,2,34,20,54,6};

        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
        // Adds the sum of all elements
        int sum = 0;        
        for(int i:arr)
        {
            sum+=i;
        }
        // Prints the sum
        System.out.println("The sum of all the array elements is: "+sum);
    }
}

Output:

The array elements are[12, 2, 34, 20, 54, 6]
The sum of all the array elements is: 128

Method-2: Java Program to Find the Sum of All the Elements of an Array By Dynamic Initialization of Array Elements

Approach:

  • Ask the user to enter the array size and store it.
  • Create an empty array of the specified size.
  • Ask the user to enter the elements.
  • Use a for loop to store the elements. Find the sum of all elements by iterating using a for loop.
  • Print the array elements.
  • Print the sum.

Program:

import java.util.Arrays;
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Taking size as input from the user
        System.out.println("Enter the array size : ");
        int size = scan.nextInt();

        // Creating the array
        int arr[] = new int[size];

        // Entering the array elements
        System.out.println("Enter array elements : ");
        int sum = 0;        
        for(int i=0;i<size;i++)
        {
            arr[i] = scan.nextInt();
            // Adds the sum of all elements
            sum+=arr[i];
        }

        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
        
        // Prints the sum
        System.out.println("The sum of all the array elements is: "+sum);
    }
}

Output:

Enter the array size 5
Enter array elements 1 2 3 4 5
The array elements are[1, 2, 3, 4, 5]
The sum of all the array elements is: 15

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: