Printing Numbers In Spiral Matrix Form

Posted on December 12, 2014 by

Given a input of N, prints 1 to (N * N) numbers in spiral matrix order.

E.g.: Input: 5

Output:

1	16	15	14	13	
2	17	24	23	12	
3	18	25	22	11	
4	19	20	21	10	
5	6	7	8	9

Code

import java.util.Scanner;

class SpiralMatrix {
    public static void main(String args[] ) throws Exception {
      	    
      	System.out.println("Enter N*N Matrix size:");
	    int N = new Scanner(System.in).nextInt();
	     
	    int current = 1;
		// Start in the corner
		int x = 0, y = 0, dx = 1, dy = 0;
		int spiral[][]= new int[N][N];
		
		while (current <= N*N) {
		    // Go in a straight line
		    spiral[x][y] = current++;
		    int nx = x + dx, ny = y + dy;
		   
		    // When you hit the edge...
		    if (nx < 0 || nx == N || ny < 0 || ny == N || spiral[nx][ny] != 0) {
		        // ...turn right
		        int t = dy;
		        dy = dx;
		        dx = -t;
		    }
		    x += dx;
		    y += dy;
		}
		
		for (int i = 0; i < N; i++) {
	    	for (int j = 0; j < N; j++) {
	        	System.out.print(spiral[i][j]+"\t");
	    	}
	    	System.out.print("\n"); 
	    }
   }
}

Raw Code

This website is made possible by displaying online advertisements to our visitors.
Please consider supporting by disabling your ad blocker.
loading comments...