Java sum of no

www.laut‮.irut‬com
Java sum of no

To find the sum of a series of numbers in Java, you can use a loop to iterate over the numbers and add them together.

Here's an example of how to find the sum of a series of numbers using a loop:

int sum = 0;
for (int i : new int[]{1, 2, 3, 4, 5}) {
    sum += i;
}
System.out.println(sum);  // prints 15

In this example, we're using a for-each loop to iterate over the elements of an array of int values and add them to the sum variable. The output will be the sum of the elements of the array: 15.

You can also use the stream API introduced in Java 8 to find the sum of a series of numbers. Here's an example of how to do this:

int sum = Arrays.stream(new int[]{1, 2, 3, 4, 5}).sum();
System.out.println(sum);  // prints 15

In this example, we're using the stream method of the Arrays class to create a stream of int values, and the sum method to sum the elements of the stream.

You can find more information about the stream API and examples of how to use it in the Java documentation.

Created Time:2017-10-17 20:18:53  Author:lautturi