Write a program to input integer
elements into an array of size 20 and
perform the following operations in java:
[15]
(i) Display largest number from
the array.
(ii) Display smallest number from
the array.
(iii) Display sum of all the
elements of the array.
Answers
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a[]=new int[20];
int max=0,min=0,sum=0;
System.out.println("Enter the 1st element of the array");
a[0]=in.nextInt();
max=a[0];
min=a[0];
System.out.println("Enter the remaining elements of the array:");
for(int i=1;i<a.length;i++)
{
a[i]=in.nextInt();
if(a[i]>max)
max=a[i];//If any element of the array is found greater than max it is stored in max
if(a[i]<min)
min=a[i];//If any element of the array is found smaller than min it is stored in min
}
for(int i=0;i<a.length;i++)
{
sum=sum+a[i];//For calculating the sum
}
System.out.println("The greatest element="+max);
System.out.println("The smallest element="+min);
System.out.println("The sum of the elements of the array="+sum);
in.close();
}
}
Output is attched.