Java Program to Find Sum of Array Elements (original) (raw)
Last Updated : 26 Jan, 2023
Given an array of integers. Write a Java Program to find the sum of the elements of the array.
Examples:
Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6
Input : arr[] = {15, 12, 13, 10} Output : 50 15 + 12 + 13 + 10 = 50
An array isa data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be quickly sorted or searched. All the items of the array are stored at contiguous memory locations.
Array Elements: Each item of an array is an Element. All the elements in an array must be of the same type.
Algorithm
- Initialize an array arr and a variable sum.
- Set the value of sum=0.
- Start a for loop from index 0 to the length of the array - 1.
- In every iteration, perform sum = sum + arr[i].
- After the termination of the loop, print the value of the sum. Java `
// Java Program to find sum of elements in a given array class Test { static int arr[] = { 12, 3, 4, 15 };
// method for sum of elements in an array
static int sum()
{
int sum = 0; // initialize sum
int i;
// Iterate through all elements and add them to sum
for (i = 0; i < arr.length; i++)
sum += arr[i];
return sum;
}
// Driver method
public static void main(String[] args)
{
System.out.println("Sum of given array is "
+ sum());
}
}
`
Output
Sum of given array is 34
Time Complexity: O(n)
Auxiliary Space: O(1)
One Line Solution
- One Line solution using Java inbuilt method
- import java.util.Arrays is an important statement
Syntax
Arrays.stream(arrayName).sum();
Java `
import java.util.Arrays; // import Arrays class to use inbuilt sum() method import java.io.*;
class GFG { public static void main (String[] args) { int [] nums = {1,2,3,4,5}; int sum = Arrays.stream(nums).sum(); System.out.println(sum); } }
`
Time Complexity: O(n)
Auxiliary Space: O(1)
Using Recursion:
Java `
//sum of array elements using //Recursion
import java.io.*;
class GFG { public static int sumArray(int[] arr, int n) { if (n == 0) { return arr[n]; } return arr[n] + sumArray(arr, n-1); }
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int n = arr.length;
int sum = sumArray(arr, n-1);
System.out.println("Sum of the elements in the array: " + sum);
}
}
//This code is contributed Vinay Pinjala.
`
Output
Sum of the elements in the array: 15
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
- Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++ As you see, the array of size 9 holds ele 6 min read
- Java Program to Print the kth Element in the Array We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print 2 min read
- Java Program to Increment All Element of an Array by One Given the array, the task is to increment each element of the array by 1. Complete traversal is required for incrementing all the elements of an array. An optimized way to complete a given task is having time complexity as O(N). Examples: Input : arr1[] = {50, 25, 32, 12, 6, 10, 100, 150} Output: ar 4 min read
- Java Program for Equilibrium index of an array Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an array A: Example : Input: A[] = {-7, 1, 5, 2, -4, 3, 0} Output: 3 3 is an equilibrium index, because: A[0] + A[1] + A[2] = A[4] + A[5] + A[6] 5 min read
- Java Program to Store Even & Odd Elements of an Array into Separate Arrays Given an array with N numbers and separate those numbers into two arrays by odd numbers or even numbers. The complete operation required O(n) time complexity in the best case. For optimizing the memory uses, the first traverse through an array and calculate the total number of even and odd numbers i 2 min read
- Java Program for Maximum equilibrium sum in an array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[]. Examples : Input : arr[] = {-1, 2, 3, 0, 3, 2, -1} Output : 4 Prefix sum of arr[0..3] = Suffix sum of arr[3..6] Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2} Output : 7 Prefix sum of arr[0..3] = Su 4 min read
- Java Program for Sum the digits of a given number Given a number, find the sum of its digits.Example : Input : n = 687Output : 21 Input : n = 12Output : 3 1. Iterative: Java Code // Java program to compute // sum of digits in number. import java.io.*; class GFG { /* Function to get sum of digits */ static int getSum(int n) { int sum = 0; while (n ! 3 min read
- Java Program to Find a triplet such that sum of two equals to third element Given an array of integers, you have to find three numbers such that the sum of two elements equals the third element.Examples: Input: {5, 32, 1, 7, 10, 50, 19, 21, 2}Output: 21, 2, 19 Input: {5, 32, 1, 7, 10, 50, 19, 21, 0}Output: no such triplet exist Question source: Arcesium Interview Experience 5 min read
- Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve 3 min read
- Java Program for Mean of range in array Given an array of n integers. You are given q queries. Write a program to print the floor value of mean in range l to r for each query in a new line. Examples : Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4 Output : 2 3 3 Here for 0 to 2 (1 + 2 + 3) / 3 = 2 Input : arr[] = {6, 7, 8, 10} q = 2 0 4 min read