Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class Arrays{
- private static String getInput(String prompt){
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- System.out.print(prompt);
- System.out.flush();
- try{
- return stdin.readLine();
- }catch(Exception e){
- return "Error: " + e.getMessage();
- }
- }
- public static void main(String args[]) {
- String input = getInput("Enter the number of elements: ");
- int n = Integer.parseInt(input);
- int [] a = new int [n];
- int [] even = new int [n];
- int [] odd = new int [n];
- int i, countEven = 0, countOdd = 0;
- for(i = 0; i<n; i++){
- input = getInput("Enter an element: ");
- a[i] = Integer.parseInt(input);
- if (a[i]%2 == 0)
- even[countEven++] = a[i];
- else
- odd[countOdd++] = a[i];
- }
- int smallEven = even [0];
- for(i = 0; i<countEven; i++){
- System.out.print(even[i] + " ");
- if(even[i] < smallEven)
- smallEven = even[i];
- }
- System.out.println(" are even. The smallest out of the "+countEven+" is "+smallEven);
- int largeOdd = odd[0];
- for(i = 0; i<countOdd; i++){
- System.out.print(odd[i] + " ");
- if(odd[i] > largeOdd)
- largeOdd = odd[i];
- }
- System.out.println("are odd. The largest out of the "+countOdd+" is "+largeOdd);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement