**Question:**

Write a Program in Java to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input.

For example: if n = 4, then n*n = 16, hence the array will be filled as given below.

**Note:** This program is also known as Spiral Matrix

**See:** Java program to print a variation of Circular (Spiral) Matrix

**Solution:**

/** * The class Circular_Matrix creates a Square Matrix of size n*n and fills it in a circular fashion * @author : www.javaforschool.com * @Program Type : BlueJ Program - Java */ import java.util.*; class Circular_Matrix { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements : "); int n = sc.nextInt(); int A[][] = new int[n][n]; int k=1, c1=0, c2=n-1, r1=0, r2=n-1; while(k<=n*n) { for(int i=c1;i<=c2;i++) { A[r1][i]=k++; } for(int j=r1+1;j<=r2;j++) { A[j][c2]=k++; } for(int i=c2-1;i>=c1;i--) { A[r2][i]=k++; } for(int j=r2-1;j>=r1+1;j--) { A[j][c1]=k++; } c1++; c2--; r1++; r2--; } /* Printing the Circular matrix */ System.out.println("The Circular Matrix is:"); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(A[i][j]+ "\t"); } System.out.println(); } } }

**Working:**

We will take a variable ‘k’ which will begin with 1 and will do the work of filling. i.e. for every cell, it will increase by 1. The below given processes will repeat till the value of ‘k’ becomes ‘n*n’

- C
_{1}denotes the index of the column from where we have to begin. Hence its initial value will be 0. - C
_{2}denotes the index of the column where we have to end. Hence its initial value will be ‘n-1’ (n is the size of the matrix). - R
_{1}denotes the index of the row from where we have to begin. Hence its initial value will be 0. - R
_{2}denotes the index of the row where we have to end. Hence its initial value will be ‘n-1’ (n is the size of the matrix).

The filling up of the matrix in circular fashion will consist of 4 different steps which will continue till the matrix is filled completely.

**Step 1:** We will fill the elements of Row 0 (**R _{1}**), starting from Column 0 (

**C**) till ‘n-1’ (

_{1}**C**). The cells which will be filled are marked in the image above in yellow color.

_{2}The elements will be accessed as follows:

**A[R1][i]**, where ‘i’ will go from

**C1**to

**C2**(A[ ][ ] is the array)

**Step 2:** Now, we will fill the elements of Column ‘n-1’ (**C _{2}**), starting from Row

**R**till

_{1}+1**R**. The cells which will be filled are marked in the image above in grey color.

_{2}The elements will be accessed as follows:

**A[j][C2]**, where ‘j’ will go from

**R1+1**to

**R2**(A[ ][ ] is the array)

**Step 3:** Next we will fill the elements of Row ‘n-1’ (**R _{2}**), starting from Column

**C**till

_{2}-1**C**. The cells which will be filled are marked in the image above in green color.

_{1}The elements will be accessed as follows:

**A[R2][i]**, where ‘i’ will go from

**C2-1**to

**C1**(A[ ][ ] is the array)

**Step 4: **Now, we will fill the elements of Column **C _{1}**, starting from Row

**R**till

_{2}-1**R**. The cells which will be filled are marked in the image above in blue color.

_{1}+1The elements will be accessed as follows:

**A[j][C1]**, where ‘j’ will go from

**R2-1**to

**R1+1**(A[ ][ ] is the array)

The above 4 steps will now repeat with the inner matrix which is marked in white color in the above image. For the inner matrix,

C_{1} will increase by 1 i.e. it will be C_{1}+1.

C_{2} will decrease by 1 i.e. it will be C_{2}-1.

R_{1} will increase by 1 i.e. it will be R_{1}+1.

R_{2} will decrease by 1 i.e. it will be R_{2}-1.

The above processes will repeat till we have filled in ‘n*n’ values.

**Output:**

The advices on this site are just great! Now here’s my trouble.

The entire program is done in the main method. How can it be called an oops concept? Is it acceptable in an ISC practical examination?

Better to do using functions, implementing OOP. But the program done only within main() is also perfectly acceptable in ISC

thank u so much for such an easy coding which is not usually found on internet

thank you sir.

Thanks a lot for such a clear and easy explanation.

Thank you sir so much very nice explanation

Thank you so much very nice explanation

Thanks allot sir..fr this prgm

Great! Thank you so much

Thank you for a very readable solution!

thank you so much sir.. its very useful….

sir..thank you so much..i never understood this program anywhere in school or coaching..bt you have explained it beautifully..thnk u so much ðŸ™‚

– a isc student

thank you so much. It was so helpful and your instructions we amazing. Keep it up (y).