instead of while loop we can give the while(count > 0). and then no need of if(count>0) everywhere. Isnt it ok?
Instead of while loop we can give the while(count > 0). and then no need of if(count>0) everywhere. Isnt it ok?
public class SpiralPrint { public void display(int[][] arr,int m,int n){ int top = 0; int bottom = m-1; int left = 0; int right = n-1; int count = m*n; int dir = 1; while (count>0) { // if (count > 0) { if (dir == 1) { for (int i = left; i <= right; i++) { System.out.print(arr[top][i]+" “); count–; } top++; dir=2; } // } // if (count >0){ if (dir==2){ for (int i=top;i<=bottom;i++){ System.out.print(arr[i][right]+” “); count–; } right–; dir=3; } // } // if (count>0){ if (dir==3){ for (int i=right;i>=left;i–){ System.out.print(arr[bottom][i]+” “); count–; } bottom–; dir=4; } // } // if (count>0){ if (dir==4){ for (int i=bottom;i>=top;i–){ System.out.print(arr[i][left]+” "); count–; } left++; dir=1; } // } } } public static void main(String[] args) { int arr[][] = {{00,01,02,03,04}, {10,11,12,13,14}, {20,21,22,23,24}, {30,31,32,33,34}, {40,41,42,43,44}}; int m=5,n=5; SpiralPrint s = new SpiralPrint(); s.display(arr,m,n); } }
Hey @kushalasarkar
please share your code after saving it on ide.codingblocks.com so that its legible