Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java Compute Sum and Average of Array Elements

class Main {
 public static void main(String[] args) {
   int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};
   int sum = 0;
   Double average;
      // access all elements using for each loop
   // add each element in sum
   for (int number: numbers) {
     sum += number;
   }
     // get the total number of elements
   int arrayLength = numbers.length;
   // calculate the average
   // convert the average from int to double
   average =  ((double)sum / (double)arrayLength);
   System.out.println("Sum = " + sum);
   System.out.println("Average = " + average);
 }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Employee Java. 
Java :: split with multiple condition in java 
Java :: How to read and write XML files 
Java :: Random Float in java 
Java :: java ceil 
Java :: type of exception in java 
Java :: jasypt-spring-boot 
Java :: switch statement java 
Java :: public class HelloWorld{ public static void main(String []args){ System.out.println("Hello World"); } } 
Java :: hashmap 
Java :: main method in java 
Java :: java character for end of file 
Java :: list constructor java 
Java :: remove whitespaces 
Java :: stringbuilder java 
Java :: combinations in java 
Java :: how to check number of messages in kafka topic 
Java :: how to make a text field required in android studio 
Java :: mongodb check if field exists java 
Java :: Java FileOutputStream to write data to a File 
Java :: hasNext for scanner class java 
Java :: bukkit random 
Java :: java check if year is leap 
Java :: jsp check if request parameter exists 
Java :: split string into array java 
Java :: How to perform an iterative inorder traversal through a binary search tree, in Java? 
Java :: How to output error in java 
Java :: math square java 
Java :: how to call same activity again in android 
Java :: spring security antmatchers id 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =