Answers for "java how to find sum of n array"

2

sum of all numbers in array java

int[] a = {10,20,30,40,50};
int sum = IntStream.of(a).sum();
System.out.println("The sum is " + sum);
Posted by: Guest on December-11-2020
0

sum of arrays numbers in java

public static void main(String[] args) {

String str = "a1b2c3d4";
char[] arr = str.toCharArray(); // stringi char array return etti.

System.out.println(Arrays.toString(arr)); // [a, 1, b, 2, c, 3, d, 4]

int sum = 0;    // will contain sum of all digits
  for(char each  : arr ){ // '0'
   if( Character.isDigit(each) ){  
  // isDigit(): identifies if the given character is digit
    sum += Integer.parseInt(""+each); } 
}
   System.out.println(sum); // 10
Posted by: Guest on June-04-2021

Code answers related to "java how to find sum of n array"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language