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.
Advertisements
Question
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.
Answer in Brief
Advertisements
Solution
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 <arr.length; i++)
{
if (arr[i] >max)
max = arr[i];
sum += arr[i];
}
System.out.println("Largest Number = " +max);
System.out.println("Sum = " +sum);
}
}shaalaa.com
Is there an error in this question or solution?
