Page 381 - CA_Blue( J )_Class10
P. 381
To accept five numbers in the array and display the smallest and the largest element in the array.
Program 2
Also print the sum of all the elements.
1 import java.util.*;
2 class array_largesmall
3 { public static void main()
4 { Scanner sc= new Scanner(System.in);
5 int ar[]=new int[5];
6 int i,l,s,sum=0;
7 double avg;
8 for (i=0; i<5; i++)
9 { System.out.print("Enter a number : ");
10 ar[i] =sc.nextInt();
11 }
12 l=ar[0]; s=ar[0];
13 for (i=0; i<5; i++)
14 { if(l<ar[i])
15 { l=ar[i]; }
16 if(s>ar[i])
17 { s=ar[i]; }
18 sum=sum+ar[i];
19 }
20 System.out.println("Largest element:"+l);
21 System.out.println("Smallest element:"+s);
22 System.out.println("sum:"+sum);
23 }
24 }
You will get the following output:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Largest element: 5
Smallest element: 1
sum: 15
379
Arrays 379

