CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

Show leargest,smallest and Sum of Number in a array in JAVA

JAVA November 21, 2025 3 views 0 likes
Write a program to input integer elements into an array of size 20 and perform the following operations:
  1. Display largest number from the array.
  2. Display smallest number from the array.
  3. Display sum of all the elements of the array.

Answer:


import java.util.Scanner;

public class MinMaxSum
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int arr[] = new int[20];

        System.out.println("Enter 20 numbers:");
        for (int i = 0; i < 20; i++) {
            arr[i] = in.nextInt();
        }

        int min = arr[0], max = arr[0], sum = 0;

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < min)
                min = arr[i];

            if (arr[i] > max)
                max = arr[i];

            sum += arr[i];
        }

        System.out.println("Largest Number = " + max);
        System.out.println("Smallest Number = " + min);
        System.out.println("Sum = " + sum);
    }
}


OUTPUT


Min Max Sum (Java → JS)

Min Max Sum

3 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285