Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 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 count =0;
  8. for (int i =0; i<array.length; i++)
  9. {
  10. if(array[i]%2 != 0)
  11. {
  12. count++;
  13. }
  14. }
  15. if (odd)
  16.  
  17. return count;
  18. else
  19. return array.length-count;
  20.  
  21. }
  22.  
  23. public static int[] getAllEvens(int[] array)
  24. {
  25. int [] evenArray = new int [countEm(array,false)];
  26. int j = 0;
  27. for (int i=0; i<array.length && j < evenArray.length; i++)
  28. {
  29. if(array[i]%2==0)
  30.  
  31. {
  32. evenArray[j]=array[i];
  33. j++;
  34. }
  35. }
  36.  
  37. return evenArray;
  38. }
  39.  
  40. public static int[] getAllOdds(int[] array)
  41. {
  42. int [] oddArray = new int [countEm(array,true)];
  43. int j = 0;
  44. for (int i=0; i<array.length && j < oddArray.length; i++)
  45. {
  46. if(array[i]%2 != 0)
  47.  
  48. {
  49. oddArray[j]=array[i];
  50. j++;
  51. }
  52. }
  53.  
  54. return oddArray;
  55. }
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement