Home / Programs / Write a program to input integer elements into an array of size 20 and perform the following operations: Display largest number from the array. Display smallest number from the array. Display sum of all the elements of the array
Programming Example

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

👁 130 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:


Expected Output:


Program Code

import java.util.Scanner;

public class RAnsariSDAMinMaxSum
{
    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

Enter 20 numbers:
12 43 56 45 6 3 4 5 12 44 23 23 45 22 11 23 45 78 99 76 33
Largest Number = 99
Smallest Number = 3
Sum = 675
Press any key to continue . . .

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.