InterviewSolution
| 1. |
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; class LargeSmallSum { public static void main(String args[ ]) { int n; int max, min, sum = 0; int i, j; Scanner s = new Scanner(System.in); System.out.print(“Enter no. of elements you want in array:”); n = s.nextlnt(); int a[ ] = new int[n]; System.out.println(“Enter all the elements:”); for (i = 0; i < n; i+ +) { a[i] = s.nextlntO; } max = a[0]; min = a[0]; for(i = 0; i < n ; i + +) { if(a[i] > max) { max = a[i]; } if (a[i] < min) { min = a[i]; } } System.out.println(“Maximum Number is:”+max); System.out.println(“Smallest Number is:” +min); for(i = 0; i < n; i+ +) { sum = sum + a[i]; } System.out.println(“Sum of the Numbers is:” +sum); } } |
|