write a java program
Define a class to declare an array of size 20 of double datatype, accept the elements into
the array and perform the following:
Calculate and print the sum of all the elements.
Calculate and print the highest value of the array
Answers
Answered by
2
Answer:
import java.util.Scanner;
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] array = new double[20];
double sum = 0;
System.out.println("Enter all 20 elements in the array:");
for(int i = 0; i < array.length; i++){
array[i] = sc.nextDouble();
sum = sum + array[i];
}
System.out.println("\n---THE ARRAY YOU ENTERED---");
for(int i = 0; i < array.length; i++){
System.out.print(array[i] + " ");
}
System.out.println("The sum of all elements in the array is " + sum);
Arrays.sort(array);
System.out.println("The highest value in the array is " + array[array.length-1]);
}
}
Explanation:
Similar questions