Quick Print – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
siteicon
Solved Problem Solved Problem#JAVA#7262

Problem Statement - 07A-2017

Write a program to input integer elements into an array of size 20 and perform the following operations:
(i) Display largest number from the array.
(ii) Display smallest number from the array.
(iii) Display sum of all the elements of the array.

Solution

import java.util.Scanner;

 public class ArrayOperations {
 public static void main(String[] args) {
 Scanner scanner = new Scanner(System.in);
 int[] numbers = new int[20];
 System.out.print("Enter 20 numbers: ");
 for (int i = 0; i < 20; i++) {
 numbers[i] = scanner.nextInt();
 }

 int smallest = numbers[0];
 int largest = numbers[0];
 int sum = 0;
 for (int i = 0; i < 20; i++) {
 if (numbers[i] < smallest) { 17 smallest = numbers[i]; 18 } 19 if (numbers[i] > largest) {
 largest = numbers[i];
 }
 sum = sum + numbers[i];
 }
 System.out.println("Smallest = " + smallest);
 System.out.println("Largest = " + largest);
 System.out.println("Sum = " + sum);
 }
 }

Sample Output

Enter 20 numbers: 3 9 1 50 8 5 2 8 6 9 7 5 4 9 7 9 2 67 2 32
Smallest = 1
Largest = 67
Sum = 245