Java sum of array
In this post, you will learn how to find the sum of an array using the Java programming language.
Java array is an object which contains elements of a similar data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Here, we mention different ways to calculate the sum of an array in Java.
Java find sum of array elements
First, we initialize an array arr and a variable sum and set the value of sum=0. To find the sum of all the elements in the array, we simply iterate the array using the for loop and add each element to the sum variable. After the termination of the loop, we print the value of the sum.
public class SumOfArray {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {2,3,4,5,6,7};
int sum = 0;
//Loop through the array
for (int i = 0; i < arr.length; i++) {
// calculate sum of elements
sum = sum + arr[i];
}
System.out.println("Sum of all the elements of an array: " + sum);
}
}
Output of the above code:
Sum of all the elements of an array: 15
Sum of array elements in java using scanner
In the given Java program, we ask the user to provide the size of an array and enter all the elements of that array. Then, we calculate the sum of elements of that array using a for loop.
import java.util.Scanner;
public class SumOfArray
{
public static void main(String[] args)
{
int n, sum = 0;
Scanner scn = new Scanner(System.in);
System.out.print("Enter no. of elements:");
n = scn.nextInt();
int arr[] = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
arr[i] = scn.nextInt();
sum = sum + arr[i];
}
System.out.println("Sum of all the elements of array:"+sum);
}
}
Output of the above code:
Enter no. of elements:5
Enter all the elements:
8 4 2 2 7
Sum of all the elements of array:23
Sum of array in Java using inbuilt function
Here, we call the of() method of the IntStream class to get a stream of array elements. And, then use the sum() method to get the sum of array in Java.
import java.util.stream.IntStream;
public class SumOfArray {
public static void main(String[] args) {
//Initialize array
int[] a = {8,4,2,2,7};
int sum = IntStream.of(a).sum();
System.out.println("Sum of all the elements of array: " + sum);
}
}
Output of the above code:
Sum of all the elements of an array: 23
Find sum of array in Java 8 using stream() method
Java 8 introduces the Stream API. The Stream API provides many methods which can be pipelined to produce the desired result. We can use the sum() method of the Steam API to get a sum of all array elements.
import java.util.Arrays;
public class SumOfArray
{
public static void main(String[] args)
{
int[] arr = {21,3,4,8,2};
int sum = Arrays.stream(arr).sum();
System.out.println("The sum of all the array elements: " + sum);
}
}
Output of the above code:
The sum of all the array elements: 38
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