Sum of array elements in Java
In this post, you will learn how to get the sum of array elements using the Java programming language. Such a type of logical question is generally asked in a programming interview. There are different ways to get the sum of array elements in Java. Here, we have mentioned most of them -
Sum of array elements using forloop
Here, we are using the forloop to loop through the array and add the value of the element in each iteration to a variable sum.
public class FindSumOfArray {
public static void main(String[] args) {
// Initializing array
int [] arr = new int [] {53, 31, 11, 16, 25};
int sum = 0;
// Calculate sum of elements
// Using loop through the array
for(int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
System.out.println("Sum of the elements of the array: " + sum);
}
}
Output of the above code:
Sum of the elements of the array: 136
Sum of array elements using user input
In the given example, we have asked the user to enter the array elements. Then we have used the forloop to get the sum of all the array elements.
import java.util.Scanner;
public class FindSumOfArray
{
public static void main(String[] args)
{
int n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Please enter no. of elements : ");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements : ");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println("Sum:"+sum);
}
}
Output of the above code:
Please enter no. of elements : 5
Enter all the elements :
22
10
4
5
6
Sum:47
Sum of array elements using Arrays.stream
Here, we have used the stream method of java.util.Arrays class to get a stream of array elements. The sum method on this stream will provide the sum of elements in the array.
import java.util.Arrays;
import java.util.stream.IntStream;
public class FindSumOfArray {
public static void main(String[] args) {
int[] array = { 12, 4, 2, 6, 11, 10, 9 };
// get stream of elements
IntStream stream = Arrays.stream(array);
// Calculate sum of elements using stream
int sum = stream.sum();
System.out.println("Sum of the elements of the array : " + sum);
}
}
Output of the above code:
Sum of the elements of the array : 54
Sum of all numbers in array using IntStream method
Java IntStream class is a specialization of Stream interface for int primitive. It returns a sequential ordered stream whose elements are the specified values. Calling the sum method on the returned stream provides the sum of array elements.
import java.util.Arrays;
import java.util.stream.IntStream;
// Get sum of array
public class FindSumOfArray {
public static void main(String[] args) {
// Initializing array
int[] array = { 12, 4, 2, 6, 11, 10, 9 };
// get stream of elements
IntStream stream = Arrays.stream(array);
// Calculate sum of elements using stream
int sum = stream.sum();
System.out.println("Sum of the elements of the array : " + sum);
}
}
Output of the above code:
Sum of the elements of the array : 54
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java