Q1.Given an array int[] array={1,2,3,4,5}, write down the code to calculate the sum and average of the array.
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5 };
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
double average = (double) sum / (double) array.length;
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
}
}
Comments
Leave a comment