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
Write a program to input integer elements into an array of size 20 and perform the following operations:
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 . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.