Advertisement
Guest User

HW1 David Peikrishvili

a guest
Feb 9th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. //David Peikrishvili
  2. //2/8/2016
  3.  
  4. import java.util.Scanner;
  5. public class HW1
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         Scanner s = new Scanner(System.in);
  10.         System.out.println("Please enter a number");
  11.         int n = s.nextInt();
  12.         int arr[] = new int[n];
  13.         System.out.println("Enter ");
  14.         for(int i=0; i<n; i++) {
  15.             arr[i]=s.nextInt();
  16.         }
  17.         int [] odd = filterOdd(arr);
  18.         try {
  19.             for(int i=0; i<n; i++) {
  20.                 System.out.println("Odd" + odd[i]);
  21.             }
  22.         } catch(ArrayIndexOutOfBoundsException e) {}
  23.         int [] even = filterEven(arr);
  24.         try {
  25.             for(int i=0; i<n; i++) {
  26.                 System.out.println("Even" + even[i]);
  27.             }
  28.         } catch(ArrayIndexOutOfBoundsException e) {}
  29.     }
  30.     public static int[] filterOdd(int[] a) {
  31.         int l = 0;
  32.         int j = 0;
  33.         for(int i=0; i<a.length; i++) {
  34.             if(a[i]%2==1) {
  35.                 l++;
  36.             }
  37.         }
  38.         int k[]=new int[l];
  39.         for(int i=0; i<a.length; i++) {
  40.             if(a[i]%2==1) {
  41.                 k[j] = a[i];
  42.                 j++;
  43.             }
  44.         }
  45.         return k;
  46.     }
  47.     public static int[] filterEven(int[] a) {
  48.         int l = 0;
  49.         int j = 0;
  50.         for(int i=0; i<a.length; i++) {
  51.             if(a[i]%2==0) {
  52.                 l++;
  53.             }
  54.         }
  55.         int k[] = new int[l];
  56.         for(int i=0; i<a.length; i++) {
  57.             if(a[i]%2==0) {
  58.                 k[j] = a[i];
  59.                 j++;
  60.             }
  61.         }
  62.         return k;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement