Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Define a class to declare an array of size 20 of the double datatype, accept the elements into the array and perform the following: Calculate and print the sum of all the elements.

Define a class to declare an array of size 20 of the 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.

Ans.

import java.util.Scanner;
public class MaxSum
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
System.out.println("Enter 20 numbers:");
for (int i = 0; i < 20; i++) { arr[i] = in.nextDouble(); } int max = arr[0], sum = 0; for (inti= 0; i max)
max = arr[i];
sum += arr[i];
}
System.out.println("Largest Number = " +max);
System.out.println("Sum = " +sum);
}
}