// write a program to generate a n * n matrix in spiral order given user input n and the elements of the matrix import java.util.Scanner; class SpiralMatrixGeneration{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the number of rows and columns of the matrix"); int rows = sc.nextInt(); int cols = sc.nextInt(); int length = rows*cols; int[][] matrix = new int[rows][cols]; int lengthArray[] = new int[length]; System.out.println("Enter the elements of the matrix"); for(int i=0;i=left;i--){ matrix[bottom][i] = lengthArray[length_index++]; } bottom--; } if(left<=right){ for(int i=bottom;i>=top;i--){ matrix[i][left] = lengthArray[length_index++]; } left++; } } } }