Output:
Click Here For Java Online Compiler
Solution:
*** Move Zeros In An Array To End-2 ***
Given array is: 1, 0, 2, 0, 3, 0, 4, 0, 5, Now it becomes: 1, 2, 3, 4, 5, 0, 0, 0, 0,
Click Here For Java Online Compiler
Solution:
class MoveZerosToEnd { static void moveZerosToEnd(int inputArray[]) { int counter = 0; for (int i = 0; i < inputArray.length; i++) { if (inputArray[i] != 0) { //Assigning inputArray[i] to inputArray[counter] inputArray[counter] = inputArray[i]; counter++; } } while (counter < inputArray.length) { inputArray[counter] = 0; counter++; } for (int i = 0; i < inputArray.length; i++) { System.out.print(inputArray[i] + ", "); } } public static void main(String[] args) { int[] array = new int[]{1, 0, 2, 0, 3, 0, 4, 0, 5}; System.out.println("*** Move Zeros In An Array To End-2 ***" + "\n"); System.out.print("Given array is: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + ", "); } System.out.println(); System.out.print("Now it becomes: "); moveZerosToEnd(array); } }
comment 0 comments:
more_vertsentiment_satisfied Emoticon