0% found this document useful (0 votes)
159 views3 pages

Spiral

This document describes a Java program to fill a square matrix in a circular (spiral) pattern. It takes the matrix size n as input and fills values from 1 to n*n. It uses four counters (c1, c2, r1, r2) to track the start and end rows and columns for filling at each step. The filling follows four steps: (1) fill row r1 from c1 to c2, (2) fill column c2 from r1 to r2, (3) fill row r2 from c2 to c1, (4) fill column c1 from r2 to r1. It repeats this process, adjusting the counters, until the whole matrix is

Uploaded by

AtharvaShukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views3 pages

Spiral

This document describes a Java program to fill a square matrix in a circular (spiral) pattern. It takes the matrix size n as input and fills values from 1 to n*n. It uses four counters (c1, c2, r1, r2) to track the start and end rows and columns for filling at each step. The filling follows four steps: (1) fill row r1 from c1 to c2, (2) fill column c2 from r1 to r2, (3) fill row r2 from c2 to c1, (4) fill column c1 from r2 to r1. It repeats this process, adjusting the counters, until the whole matrix is

Uploaded by

AtharvaShukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Program to print Circular

(Spiral) Matrix
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 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.io.*;
class Circular_Matrix
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of elements : ");
int n=Integer.parseInt(br.readLine());

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
C1 denotes the index of the column from where we have to begin. Hence its initial value will be 0.
C2 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).
R1 denotes the index of the row from where we have to begin. Hence its initial value will be 0.
R2 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 (R1), starting from Column 0 (C1) till n-1 (C2). The cells
which will be filled are marked in the image above in yellow color.
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 (C2), starting from Row R1+1 till R2. The cells
which will be filled are marked in the image above in grey color.
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 (R2), starting from Column C2-1 till C1. The cells
which will be filled are marked in the image above in green color.
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 C1, starting from Row R2-1 till R1+1. The cells which
will be filled are marked in the image above in blue color.
The 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,
C1 will increase by 1 i.e. it will be C1+1.
C2 will decrease by 1 i.e. it will be C2-1.
R1 will increase by 1 i.e. it will be R1+1.
R2 will decrease by 1 i.e. it will be R2-1.
The above processes will repeat till we have filled in n*n values.

You might also like