-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrixCode.txt
More file actions
57 lines (56 loc) · 1.78 KB
/
SpiralMatrixCode.txt
File metadata and controls
57 lines (56 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
// Write A Program to generate and print a SPIRAL MATRIX of index n, where n is the size of the matrix that is to be accepted from the user.
import java.util.*;
// Calling the library class (util)
class SpiralMatrix
{
void main()// Creating a main function
{
Scanner sc = new Scanner ( System.in );
// Creating an object of class Scanner
System.out.println(" Input the size of the spiral matrix to be generated. ");
int size = sc.nextInt();
// Taking the size of the square array input from the user
System.out.println();
System.out.println(" The spiral matrix generated is : ");
System.out.println();
int arr[][] = new int [size][size],row=0,col=size-1,master=1,fact=0,i,j,temp=0;
// Declaring and initializing the variables
while(master <= (size*size))// Conditional loop
{
fact = 1;
if(row == col)
{
arr[row][row] = master;
break;
}// Terminal condition
for( j=1;j<=2;j++)
{
for( i=row;i!=col;i+=fact)
{
arr[row][i]=master++;
}
for ( i=row;i!=col;i+=fact)
{
arr[i][col] = master++;
}
fact = -1;
temp = row;
row = col;
col = temp;
}
row++;
col--;
// Filling up the array in spiral form
}
for ( i=0;i<size;i++)
{
for ( j=0;j<size;j++)
{
System.out.print(arr[i][j]+ "\t");
}
System.out.println();
}// Printing the array
}
// End of main
}
// End of code