ICSE Java Programming 2017 Q7 Solved
Finding the sum, largest and the smallest element of an array with 20 integer elements.
import java.util.Scanner;
public class ICSEJava {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] array = new int[20];
int sum = 0;
int max = 0;
int min = 0;
System.out.println("Enter the elements of the array:");
for (int i = 0; i < 20; i++) {
System.out.println("Enter the element at the position:" + (i + 1));
array[i] = in.nextInt();
sum += array[i];
}
max = array[0];
min = array[0];
for (int i = 0; i < 20; i++) {
if (max < array[i])
max = array[i];
if (min > array[i])
min = array[i];
}
System.out.println("Sum of the elements of the array is "+sum);
System.out.println("Largest number of the array is "+max);
System.out.println("Smallest number of the array is "+min);
in.close();
}
}
No comments:
Post a Comment