Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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.
Answer:
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);
}
}