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
Java Programming Language (Article) (Program)
21
Given Input:
Expected Output:
Program:
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 . . .
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
Program:
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 . . .
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.