lundi 2 septembre 2019

How to find Odd or Even numbers in a single Integer in Java

I'm trying to solve a problem on how to extract all odd and even numbers from a single Integer.

I already tried ArrayList but I'm kind of confused.

The expected output of 6: 1,3,5 2,4,6

but the actual output of 123 is [1, 2, 3]

Here's my code from @LIttle Ancient Forest Kami at codereview:

     static ArrayList odds = new ArrayList();
    public static void main(String[] args) {
        // TODO Auto-generated method stub


                int[] simpleTable = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

                for (int i: simpleTable) {
                    if (i%2 == 0) {
                        evens.add(i);
                    }
                }
                System.out.println(evens);

                for (int i: simpleTable) {
                    if (i%2 != 0) {
                        odds.add(i);
                    }
                }
                System.out.println(odds);
    }```

/*

System.out.println("Enter Number: ");
        num = scanner.nextInt();


        ArrayList<Integer> array = new ArrayList<Integer>();

        do{
            array.add(0, num %10);
            num /= 10;
        } while  (num > 0);

        if ((num%2) == 0) {
                  System.out.println(array);
            }
        else
        {
               System.out.println(array);
        }
      } */


Aucun commentaire:

Enregistrer un commentaire