Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class OddsAndEvens
  4. {
  5. private static int countEm(int[] array, boolean odd)
  6. {
  7. int oddCount = 0;
  8. for(int i = 0; i < array.length; i++)
  9. {
  10. if(array[1] % 2 == 0)
  11. {
  12. oddCount += 1;
  13. }
  14. }
  15. if (odd)
  16. {
  17. return oddCount;
  18. }
  19. return array.length - oddCount;
  20. }
  21.  
  22. public static int[] getAllEvens(int[] array)
  23. {
  24. int[] newArray = new int[countEm(array, false)];
  25. int arrayCount = 0;
  26. for(int i = 0; i < array.length; i++)
  27. {
  28. if(array[i] % 2 == 0)
  29. {
  30. newArray[arrayCount] = array[i];
  31. arrayCount++;
  32. }
  33. }
  34. return newArray;
  35. }
  36.  
  37. public static int[] getAllOdds(int[] array)
  38. {
  39. int[] newArray = new int[countEm(array, true)];
  40. int arrayCount = 0;
  41. for(int i = 0; i < array.length; i++)
  42. {
  43. newArray[arrayCount] = array[i];
  44. arrayCount++;
  45. }
  46. return newArray;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement