1int[] a = {10,20,30,40,50};
2int sum = IntStream.of(a).sum();
3System.out.println("The sum is " + sum);
4
1public class Exercise2 {
2public static void main(String[] args) {
3int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
4int sum = 0;
5
6for (int i : my_array)
7 sum += i;
8System.out.println("The sum is " + sum);
9}
10}
1public static void main(String[] args) {
2
3String str = "a1b2c3d4";
4char[] arr = str.toCharArray(); // stringi char array return etti.
5
6System.out.println(Arrays.toString(arr)); // [a, 1, b, 2, c, 3, d, 4]
7
8int sum = 0; // will contain sum of all digits
9 for(char each : arr ){ // '0'
10 if( Character.isDigit(each) ){
11 // isDigit(): identifies if the given character is digit
12 sum += Integer.parseInt(""+each); }
13}
14 System.out.println(sum); // 10