KnowledgeBoat Logo
|
LoginJOIN NOW

Computer Applications

Write a program to initialise the given data in an array and find the minimum and maximum values along with the sum of the given elements.

Numbers: 2, 5, 4, 1, 3

Output:
Minimum value: 1
Maximum value: 5
Sum of the elements: 15

Java

Java Arrays

ICSE 2007

98 Likes

Answer

public class KboatMinMaxSum
{
    public static void main(String args[]) {
        int arr[] = {2, 5, 4, 1, 3};
        
        int max = arr[0];
        int min = arr[0];
        int sum = 0;
        
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max)
                max = arr[i];
                
            if (arr[i] < min)
                min = arr[i];
                
            sum += arr[i];
        }
        
        System.out.println("Minimum value: " + min);
        System.out.println("Maximum value: " + max);
        System.out.println("Sum of the elements: " + sum);
    }
}

Output

BlueJ output of Write a program to initialise the given data in an array and find the minimum and maximum values along with the sum of the given elements. Numbers: 2, 5, 4, 1, 3 Output: Minimum value: 1 Maximum value: 5 Sum of the elements: 15

Answered By

46 Likes


Related Questions