Write a program in java to accept 10 even integers in an array using scanner class and find the position of the largest and the smallest integer of the array without using sorting technique. print the original array, largest and smallest integer with its position in the array.
Answers
Answer: The required Java program is :-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter 10 even integers separated by spaces : ");
String[] orig1 = scan.nextLine().split(" ");
scan.close();
int[] orig2 = new int[10];
for(int i=0 ; i<10 ; i++) {
orig2[i] = Integer.parseInt(orig1[i]);
}
int large=orig2[0] , small=orig2[0];
int iSmall=0 , iLarge=0;
for(int i=1 ; i<10 ; ++i) {
if(orig2[i]>large) {
large=orig2[i];
iLarge=i;
}
if(orig2[i]<small) {
small=orig2[i];
iSmall=i;
}
}
System.out.println("\nThe original array is {"+(String.join(" , ",orig1))+"}");
System.out.println("Smallest integer is "+small+" and its position in array is "+iSmall);
System.out.println("Largest integer is "+large+" and its position in array is "+iLarge);
}
}
Note :-
The above program must be saved in a file named Main.java
Also, try-catch block can be used to handle exception.