Spiral
Spiral
1/1
import java.io.*; class spiral { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in )); System.out.println("enter the no. of rows and columns");//accepting size of array from user int n=Integer.parseInt(br.readLine()); int a[][]=new int[n][n];//declaring the array int copy=n; int k=1,r=0,c=-1;//initialising variables while(n>0)//populating the array { for(int i=1;i<=n;i++)//populating top row { a[r][++c]=k++; } for(int i=1;i<=n-1;i++)//populating right column { a[++r][c]=k++; } for(int i=1;i<=(n-1);i++)//populating last row { a[r][--c]=k++; } for(int i=1;i<=(n-2);i++)//populating left column { a[--r][c]=k++; } n=n-2;//populating inner rows and columns } for(int i=0;i<copy;i++)//displaying the array in spiral form { for(int j=0;j<copy;j++) { if (a[i][j]/10==0) System.out.print(a[i][j]+" "); else System.out.print(a[i][j]+" "); } System.out.println(); } } }