Summing elements in an array java

Java program to sum the elements of an array

In this tutorial we will see how to sum up all the elements of an array.

Program 1: No user interaction

/** * @author: BeginnersBook.com * @description: Get sum of array elements */ class SumOfArray< public static void main(String args[])< int[] array = ; int sum = 0; //Advanced for loop for( int num : array) < sum = sum+num; >System.out.println("Sum of array elements is:"+sum); > >
Sum of array elements is:160

Program 2: User enters the array’s elements

/** * @author: BeginnersBook.com * @description: User would enter the 10 elements * and the program will store them into an array and * will display the sum of them. */ import java.util.Scanner; class SumDemo < public static void main(String args[])< Scanner scanner = new Scanner(System.in); int[] array = new int[10]; int sum = 0; System.out.println("Enter the elements:"); for (int i=0; ifor( int num : array) < sum = sum+num; >System.out.println("Sum of array elements is:"+sum); > >
Enter the elements: 1 2 3 4 5 6 7 8 9 10 Sum of array elements is:55

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Comments

It is For Each Loop or enhanced for loop introduced in java 1.7 For (int num : array )
Here int is data type for num variable where you want to store all arrays data in otherwords you can say the destination where you want to give all component of arrays. Here array is the name of the array itself. So all arrays components we are giving to num variable.

Читайте также:  Does java have generics

it is a variation of for loop
basically used in collections
but you can use it to iterate through elements…
hope you get it…

If there are more than one array in program how for each loop distinguish each of them , doubt is how to use more than one for each loop in one prog having different different array list?

public class AverageIntArray < public static void main(String[] args) <
int arr[] = ;
float average=0; float sum = 0;
int length = arr.length;
for (int num : arr) <
sum = sum + num;
> average = sum/length; System.out.println(average); > >

How to write to finding sum of two 1d array with size of both array enter by the user as like …
Enter size of array :2
Enter element of first array
Stored as
X[0]=1
X[1]=2
Enter element of second array
Y[0]=3
Y[1]=6
Print shows as z[0]=x[0]+y[0]
And z[1]=x[1]+Y[1]

Источник

How to find sum of array elements in java

Sum of array elements means the sum of all the elements(or digits) in the array. Array elements can be integers( int ) or decimal numbers( float or double ).
There are different methods to calculate sum of elements in an array in java and this post discusses them all.

Method 1 : Using for loop
This is a traditional and most commonly used approach where the array is iterated using a for loop.
In each iteration, the current array element is added to a variable which holds the sum of array elements.
This variable is initialized to 0 before the start of loop. Example,

public class ArraySumCalculator { public static void main(String[] args) { int[] array = { 1, 34, 67, 23, -2, 18 }; // variable to hold sum of array elements int sum = 0; // iterate using a for loop for (int loopCounter = 0; loopCounter &lt; array.length; loopCounter++) { // get current array element int element = array[loopCounter]; // add element to sum sum += element; } System.out.println("Sum of array elements is: " + sum); } }

public class ArraySumCalculator < public static void main(String[] args) < int[] array = < 1, 34, 67, 23, -2, 18 >; // variable to hold sum of array elements int sum = 0; // iterate using a for loop for (int loopCounter = 0; loopCounter < array.length; loopCounter++) < // get current array element int element = array[loopCounter]; // add element to sum sum += element; >System.out.println(«Sum of array elements is: » + sum); > >

Sum of array elements is: 141

for loop in this program can also be replaced with a for-each loop as shown below.

Источник

Java Array Sum

To find the sum of numbers in a Java Array, use a looping technique to traverse through the elements, and accumulate the sum.

In this tutorial, we will learn how to find the sum of elements in array, using different looping statements.

Java Integer Array Sum using While Loop

In the following program, we will initialize an integer array, and find the sum of its elements using Java While Loop.

Java Program

public class ArraySum < public static void main(String[] args) < int numbers[] = ; int sum = 0; int index = 0; while (index < numbers.length) < sum += numbers[index]; index++; >System.out.println("Sum : " + sum); > >

Java Float Array Sum using For Loop

In the following program, we will initialize a float array, and find the sum of its elements using Java For Loop.

Java Program

public class ArrayExample < public static void main(String[] args) < float numbers[] = ; float sum = 0; for (int index = 0; index < numbers.length; index++) < sum += numbers[index]; >System.out.println("Sum : " + sum); > >

Java Double Array Sum using For-each Loop

In the following program, we will initialize a double array, and find the sum of its elements using Java For-each Loop.

Java Program

public class ArraySum < public static void main(String[] args) < double numbers[] = ; double sum = 0; for (double number: numbers) < sum += number; >System.out.println("Sum : " + sum); > >

Conclusion

Concluding this Java Tutorial, we learned how to find Sum of Elements in Java Array with some of the combinations of numeric datatypes for array elements and looping technique. You may use any looping technique on any of the numeric datatypes and find the Java Array sum.

Источник

Get the Sum of an Array in Java

Get the Sum of an Array in Java

  1. Find the Sum of an Array by Using a for Loop in Java
  2. Find the Sum of an Array by Using the Stream Method in Java
  3. Find the Sum of an Array by Using the reduce Method in Java
  4. Find the Sum of an Array by Using the sum Method in Java
  5. Find the Sum of an Array by Using the IntStream Interface in Java
  6. Find the Sum of an Array by Using a Compact for Loop in Java

This tutorial introduces how to find the sum of an array in Java also lists some example codes to understand the topic.

An array is defined as a collection of similar types of elements in Java. In this article, we’ll find the sum of array elements by using some built-in methods and custom codes.

Performing this operation is very common during programming. Unfortunately, Java does not provide any specific method to get the sum of an array. So, we will use some tricks to solve this issue!

Find the Sum of an Array by Using a for Loop in Java

In this example, we used a loop to traverse each array element and get thir sum parallel. This method has a simple code that requires a single loop to get the sum. Here’s the example program:

public class SimpleTesting  public static void main(String[] args)   int arr[] = new int[] 12,34,45,21,33,4>;  int sum = 0;  for (int i = 0; i  arr.length; i++)   sum+=arr[i];  >  System.out.println("Array Sum = "+sum);   > > 

Find the Sum of an Array by Using the Stream Method in Java

In this example, we used the stream() method of the Arrays class and the parallel() method to get the sum of the array elements. We passed the lambda expression to the reduce() method that actually does the sum operation. See the example below:

import java.util.Arrays; public class SimpleTesting  public static void main(String[] args)   int arr[] = new int[] 12,34,45,21,33,4>;  int sum = Arrays.stream(arr).parallel().reduce(0,(a,b)-> a + b);  System.out.println("Array Sum = "+sum);   > > 

Find the Sum of an Array by Using the reduce Method in Java

In this example, we used the reduced() method directly with the stream of arrays and get the sum of the elements. Here’s how to do it:

import java.util.Arrays; public class SimpleTesting  public static void main(String[] args)   int arr[] = new int[] 12,34,45,21,33,4>;  int sum = Arrays.stream(arr).reduce(0,(a,b)-> a + b);  System.out.println("Array Sum = "+sum);   > > 

Find the Sum of an Array by Using the sum Method in Java

Java provides the sum() method in the Stream API to get a sum of stream sequences. Here, we passed an array to the stream and got its sum by using the sum() method. See the example below:

import java.util.Arrays; public class SimpleTesting  public static void main(String[] args)   int arr[] = new int[] 12,34,45,21,33,4>;  int sum = Arrays.stream(arr).sum();  System.out.println("Array Sum = "+sum);  > > 

Find the Sum of an Array by Using the IntStream Interface in Java

This method is another solution where you can use the Intsream interface to create a stream of array elements and utilize the sum() method to get the sum in a straightforward, single-line solution. Follow the sample code here:

import java.util.stream.IntStream; public class SimpleTesting  public static void main(String[] args)   int arr[] = new int[] 12,34,45,21,33,4>;  int sum = IntStream.of(arr).sum();  System.out.println("Array Sum = "+sum);  > > 

Find the Sum of an Array by Using a Compact for Loop in Java

In this example, we used a for loop to get the sum of array elements with an additional unique process. Here, rather than creating a loop body, we just bind up into the loop signature part. We can call it a compact loop solution. You can try it if you’re not afraid of a messy code block.

public class SimpleTesting  public static void main(String[] args)   int arr[] = new int[] 12,34,45,21,33,4>;  int sum,i;  for(sum= 0, i= arr.length - 1; 0  i; sum+= arr[i--]);  System.out.println("Array Sum = "+sum);  > > 

Related Article — Java Array

Источник

Оцените статью