Question
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
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program7
{
public static void main(String[] args)
{
int i=0,smallest=0,largest=0,sum=0;
Scanner sc = new Scanner(System.in);
int[] arr = new int[20];
System.out.print("Enter 20 numbers in an array: ");
for (i = 0; i < 20; i++)
{
arr[i] = sc.nextInt();
}
smallest = arr[0];
largest = arr[0];
for (i = 0; i < 20; i++)
{
if (arr[i] < smallest)
{
smallest = arr[i];
}
if (arr[i] > largest)
{
largest = arr[i];
}
sum = sum + arr[i];
}
System.out.println(smallest+" is the Smallest number in the array");
System.out.println(largest+" is the Largest number in the array ");
System.out.println(sum+" is the Sum of the given array");
}
}