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:
- Display largest number from the array.
- Display smallest number from the array.
- Display sum of all the elements of the array
Study this program carefully to understand the logic, output, and explanation in a structured way.
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);
}
}
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 . . .