Spiral Pattern Java Program
Pattern Example 1
public class Main
{
//defining method to print the spiral pattern or matrix
static void printSpiralPattern(int size)
{
//create two variables row and col to traverse rows and columns
int row = 0, col = 0;
int boundary = size - 1;
int sizeLeft = size - 1;
int flag = 1;
//variables r, l, u and d are used to determine the movement
// r = right, l = left, d = down, u = upper
char move = 'r';
//creating a 2D array for matrix
int[][] matrix =new int [size][size];
for (int i = 1; i < size * size + 1; i++)
{
//assigning values
matrix[row][col] = i;
//switch-case to determine the next index
switch (move)
{
//if right, go right
case 'r':
col += 1;
break;
//if left, go left
case 'l':
col -= 1;
break;
//if up, go up
case 'u':
row -= 1;
break;
//if down, go down
case 'd':
row += 1;
break;
}
//checks if the matrix has reached the array boundary
if (i == boundary)
{
//adds the left size for the next boundary
boundary = boundary + sizeLeft;
//decrease the size left by 1, if 2 rotations have been made
if (flag != 2)
{
flag = 2;
}
else
{
flag = 1;
sizeLeft -= 1;
}
//switch-case to rotate the movement
switch (move)
{
//if right, rotate to down
case 'r':
move = 'd';
break;
// if down, rotate to left
case 'd':
move = 'l';
break;
// if left, rotate to up
case 'l':
move = 'u';
break;
// if up, rotate to right
case 'u':
move = 'r';
break;
}
}
}
//printing the spiral matrix or pattern
//outer for loop for rows
for (row = 0; row < size; row++)
{
//inner for loop for columns
for (col = 0; col < size; col++)
{
int n = matrix[row][col];
if(n < 10)
System.out.print(n +" ");
else
System.out.print(n +" ");
}
System.out.println();
}
}
//driver Code
public static void main(String args[])
{
//size of the array?s row and column
int size = 5;
System.out.println("Spiral Matrix or Pattern is: \n");
//calling the method that prints the spiral pattern or matrix
printSpiralPattern(size);
}
}
Pattern Example 2
import java.util.Scanner;
import java.lang.Math;
public class Main
{
//function to print the spiral pattern
public static void printPattern(int n)
{
//detrmines the boundary size of the array
int size = 2 * n - 1;
//inner loop
for(int i = 1; i <= size; i++)
{
//outer loop
for(int j = 1; j <= size; j++)
{
//calculates and prints the values for pattern
System.out.print(Math.max(Math.abs(i - n), Math.abs(j - n)) + 1 + " ");
}
System.out.println();
}
}
//driver code
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
System.out.println();
//function calling
printPattern(n);
}
}
Pattern Example 3
public class Main
{
public static void main(String args[])
{
int SIZE=10;
int i, j, N;
int[][] board = new int[SIZE][SIZE];
int left, top;
left = 0;
top = SIZE - 1;
N = 1;
for(i=1; i<=SIZE/2; i++, left++, top--)
{
//fill from left to right
for(j=left; j<=top; j++, N++)
{
board[left][j] = N;
}
//fill from top to down
for(j=left+1; j<=top; j++, N++)
{
board[j][top] = N;
}
//fill from right to left
for(j=top-1; j>=left; j--, N++)
{
board[top][j] = N;
}
//fill from down to top
for(j=top-1; j>=left+1; j--, N++)
{
board[j][left] = N;
}
}
//print the pattern
for(i=0; i<SIZE; i++)
{
for(j=0; j<SIZE; j++)
{
System.out.printf("%-5d", board[i][j]);
}
System.out.printf("\n");
}
}
}
Comments
Post a Comment