Answers for "how to find the sum of an array in java"

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
3

sum numbers in array java

public class Exercise2 {
public static void main(String[] args) {      
int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;

for (int i : my_array)
    sum += i;
System.out.println("The sum is " + sum);
}
}
Posted by: Guest on October-29-2020
2

How to find the length of an array in java

class Main {
  public static void main(String[] args) {
    // Creating an array called x.
    String[] x = new String[]{"This", "Should", "return", "4"};
    // "x.length" finds the length of the array "x".
    System.out.println(x.length);
    // returns 4
  }
}
Posted by: Guest on February-22-2020
0

add each element in an array java

import java.util.Arrays;
import java.util.Scanner;
public class SumOfElementsOfAnArray {
   public static void main(String args[]){
      System.out.println("Enter the required size of the array :: ");
      Scanner s = new Scanner(System.in);
      int size = s.nextInt();
      int myArray[] = new int [size];
      int sum = 0;
      System.out.println("Enter the elements of the array one by one ");

      for(int i=0; i<size; i++){
         myArray[i] = s.nextInt();
         sum = sum + myArray[i];
      }
      System.out.println("Elements of the array are: "+Arrays.toString(myArray));
      System.out.println("Sum of the elements of the array ::"+sum);
   }
}
Posted by: Guest on August-31-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
0

how to find the sum of an array in java

int [] arr = {1,2,3,4};
int sum = Arrays.stream(arr).sum(); //prints 10
Posted by: Guest on October-09-2021

Code answers related to "how to find the sum of an array in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language