Computer Applications

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.

Java

Java Arrays

8 Likes

Answer

import java.util.Scanner;

public class KboatSDASumMax
{
    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();
        }
        
        double max = arr[0], sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max)
                max = arr[i];
                
            sum += arr[i];
        }
        
        System.out.println("Sum = " + sum);
        System.out.println("Highest Value = " + max);      
    }
}

Output

Answered By

4 Likes


Related Questions