'I was trying to print the spiral order matrix as output for a given matrix of numbers, but the output result isn't complete. What is causing this?

I was trying to print the spiral order matrix as output for a given matrix of numbers, all seemed fine until I tried to print the output, the code prints the input till the first spiral loop but stops after printing the second loop for the first step. I am confused, what is my mistake.

edit - the loop has to go clockwise.

 import java.util.*;

 public class SpiralArray{

 public static void main (String [] args){

    System.out.println("Enter the size of the array");
    //Dimensions of array
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int c = sc.nextInt();

    int [][] spiral = new int [r][c];
    
    // elements of array
    for (int i = 0; i < r; i++){
        for (int j = 0; j < c; j++){

            spiral [i][j] = sc.nextInt();
        }
    }
     
    
    int row_start = 0;
    int row_end = r-1;
    int column_start = 0;
    int column_end = c-1;
    
    //printing spiral order matrix

    while( row_start <= row_end && column_start <= column_end){

        //1
        for (int col = column_start; col <= column_end; col++){

            System.out.print(spiral[row_start][col] + " ");
            
        } 

        row_start++;
        
        //2
        for (int row = row_start; row <= row_end; row++){

            System.out.print(spiral[row][column_end] + " ");
        }

        column_end--;

        //3
        for (int col = column_end; col >= column_start; col--){

            System.out.print(spiral[row_end][col] + " ");
            
        } 
        row_end--;

        //4
        for (int row = row_end; row >= row_start; row--){

            System.out.print(spiral[row][column_start] + " ");
        }

        column_start++;

        System.out.println();
        
         }

    }
 }

Output obtained from the code -

  Enter the size of the array       
    3 4
    1 2 3 4
    5 5 6 7
    8 9 10 1 
   
    1 2 3 4 7 1 10 9 8 5 
    5 6 5

edit expected output 1 2 3 4 7 1 10 9 8 5 5 6



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source