| 58 | import java.util.Scanner; |
| 59 | public class PrintSpiral { |
| 60 | public static void spiral(int [] [] arr) { |
| 61 | int currRow = 0; |
| 62 | int currCol = 0; |
| 63 | int rows = arr.length; |
| 64 | int cols = arr[0].length; |
| 65 | int count = 0; |
| 66 | int totalelements = rows * cols; |
| 67 | while (count < totalelements) { |
| 68 | for (int i = currCol; i < cols; i++) { |
| 69 | System.out.print(arr[currRow][i] + " "); |
| 70 | count++; |
| 71 | } |
| 72 | currRow++; |
| 73 | for (int i = currRow; i < rows; i++) { |
| 74 | System.out.print(arr[i][cols - 1] + " "); |
| 75 | count++; |
| 76 | } |
| 77 | cols--; |
| 78 | for (int i = cols - 1; i >= currCol; i--) { |
| 79 | System.out.print(arr[rows - 1][i] + " "); |
| 80 | count++; |
| 81 | } |
| 82 | rows--; |
| 83 | for (int i = rows - 1; i >= currRow ; i--) { |
| 84 | System.out.print(arr[i][currCol] + " "); |
| 85 | count++; |
| 86 | } |
| 87 | currCol++; |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | public static int [] [] takeInput() { |
| 93 | Scanner sc = new Scanner(System.in); |