Java Program to Find Sum of Array Elements


An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. An array can be defined in C with the following syntax:

int Arr[5] = {10, 20, 30, 40, 50};
/* here 10,20,30,40,50 are the elements at indices 0,1,2,3,4 respectively */

In this example, array Arr is a collection of 5 integers. Each integer can be identified and accessed by its index. The indices of the array start with 0. So, the first element of the array will have index 0, the next will have index 1 and so on.

Sum of an array is defined as the sum of all elements present in the array. For example, if the array is [1, 2, 3, 4, 5], the sum of this array is 1+2+3+4+5 = 15.

Problem Description

Write a Java program that reads an array of N elements, calculates the sum of those N elements, and displays the result to the standard output or the screen.

Problem Solution

Enter the size of array and then enter all the elements of that array. Now we calculate the sum of elements of the array with the help of for loop.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Program/Source Code

Here is the source code of the Java Program to Accept Array Elements and Calculate Sum. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Array_Sum
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int n, sum = 0;
  7.         Scanner s = new Scanner(;
  8.         System.out.print("Enter size of the array:");
  9.         n = s.nextInt();
  10.         int a[] = new int[n];
  11.         System.out.println("Enter array elements:");
  12.         for(int i = 0; i < n; i++)
  13.         {
  14.             a[i] = s.nextInt();
  15.             sum = sum + a[i];
  16.         }
  17.         System.out.println("Sum of the array is:"+sum);
  18.     }
  19. }
Program Explanation

1. The program starts by importing the java.util.Scanner class, which will be used to read user input.
2. In the main method, the program declares two integer variables: “n” and “sum“. “n” will be used to store the number of elements in the array and “sum” will be used to store the sum of all elements in the array.
3. The program creates an instance of the Scanner class named “s” and uses it to prompt the user to enter the number of elements they want in the array.
4. The user’s input is then read using the nextInt() method of the Scanner class and stored in the “n” variable.
5. The program creates an integer array named “a” with a length of “n“.
6. The program then prompts the user to enter all the elements of the array and reads each element using a for loop that iterates from 0 to n-1.
7. Within the loop, the user’s input is stored in the “a” array and added to the “sum” variable.
8. Once all elements have been read and added to the sum, the program prints the sum of all elements in the array to the console using the System.out.println() method.

Check this: BCA MCQs | Java Books

Time Complexity: O(n)
In the above program, a loop is executed to traverse the loop, so the time complexity is O(n).

Space Complexity: O(n)
Space is required to store the array elements, so the space complexity is O(n).

Run Time Testcases

Testcase 1: To find the sum of array elements, enter “4” as the size and the array elements 1, 2, 3, 4 and 5 as input.

$ javac
$ java Array_Sum
Enter size of the array: 5
Enter array elements:
1 2 3 4 5
Sum of the array is: 15

Testcase 2: To find the sum of array elements, enter “5” as the size and the array elements 4, 2, 1 and 3 as input.

$ javac
$ java Array_Sum
Enter size of the array: 4
Enter array elements:
4 2 1 3
Sum of the array is: 10

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.


Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.