Quetion 19
Quetion 19
Write a program to declare a Square Matrixx a[][] of order m*n where M is the number of rows
and N is the number of columns such that ‘m’ and ‘n’ both must be greater than 2 and less than
10, Accept the values of m and n as user input.
Fill a Matrixx of size M into in in a spiral (or circular) fashion (clockwise) with natural numbers
from 1 to m*n.
Input: m=4, n=4
Output:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
System.out.println("Spiral Matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}
Program 19: Variable Description:
Variable Data type Purpose
a[] int Array variBLE
m int Number of rows
n int Number of columns
value int Value to be filled in the matrix
top int To fill top row
bottom int To fill bottom row
rigth int To fill ritgh column
left int To fill left column
i,j int Loop variables