Java Program to Print Spiral Pattern7 Jan 2025 | 5 min read The spiral pattern (or matrix in spiral form) is frequently asked in Java interviews and academics. In this section, we will create a Java program to create a spiral pattern or spiral matrix. What is Spiral Matrix or Spiral Pattern?A spiral pattern is a number pattern that can be represented in matrix form. It is made up of a 2D array (m*n). Pattern may represent in two forms, either clockwise or anticlockwise. In order to print a matrix in spiral pattern (clockwise) form, we need to follow the following traversal order:
In order to print the spiral pattern (anticlockwise), reverse the above traversal order. Let's understand the pictorial representation of the spiral patterns, as shown in the following figure. ![]() For example, consider the following 5*5 matrix: ![]() The above matrix can be represented in spiral form as follows: ![]() We can follow any of the approaches to print the spiral pattern:
Throughout this section, we have used the iterative approach. What should be the approach?
Spiral Pattern Java ProgramSpiralPaternExample1.java Output: ![]() Let's see another spiral pattern. In the following program, we have used the following approach: Starting the indexing from i = 1 and j = 1. It can be observed that every value of the required matrix will be max(abs(i - n), abs(j - n)) + 1. SpiralPatternExample2.java Output: ![]() SpiralPatternExample3.java Output: ![]() Print a Given Matrix in Spiral FormThere are many approaches that can be used to print a matrix in spiral form. SpiralToMatrixExample.java Output: 10 20 30 40 50 60 120 180 170 160 150 140 130 70 80 90 100 110 |
In this program, we will create a circular linked list and insert every new node at the beginning of the list. If the list is empty, then head and tail will point to the newly added node. If the list is not empty, then we...
6 min read
In this program, we need to check whether the given matrix is an identity matrix. Identity Matrix A matrix is said to be an identity matrix if it is a square matrix in which elements of principle diagonal are ones, and the rest of the elements are...
4 min read
How to reserve a string in Java without using reverse function There are following ways to reverse a string in Java: Using for loop Using While loop Using static method Using For loop Example to reverse string in Java by using for loop In the following example, we have used for loop to...
2 min read
In this program, we will create a doubly linked list and delete a node from the beginning of the list. If the list is empty, print the message "List is empty". If the list is not empty, we will make the head to point to...
6 min read
5432* 543*1 54*21 5*321 *4321 Algorithm: STEP 1: START STEP 2: DEFINE i, j. STEP 3: SET lines=5 STEP 4: SET i=1. REPEAT STEP 5 to STEP 10 UNTIL i <= lines STEP 5: SET j=lines STEP 6: REPEAT STEP 7 and 8 UNTIL j >= 1 STEP 7: IF j!=i then PRINT j ...
1 min read
It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going...
7 min read
Java program to print the following pattern on the console Algorithm: STEP 1: START STEP 2: DEFINE i, j STEP 3: SET n=6 STEP 4: SET i=n. REPEAT STEP 5 to STEP 7 UNTIL i>0. STEP 5: SET j=0. REPEAT STEP 6 UNTIL j STEP 6: PRINT * and SET j=j+1 STEP...
2 min read
Java String to Date We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes. To learn this concept well, you should visit DateFormat and SimpleDateFormat classes. Java String to Date Example Let's see the simple code to convert String to Date in java. import java.text.SimpleDateFormat; import...
2 min read
Write a ? File: RemoveChar .java public class ReverseStringPreserveSpace { static void reverseString(String input) { char[] inputArray = input.toCharArray(); char[] result = new char[inputArray.length]; for (int i = 0; i < inputArray.length; i++) { if (inputArray[i] == ' ') { result[i] = ' '; } } int j = result.length - 1; for...
1 min read
An array is given that contains integers. The task is to find the total count inversions in the given array. Total count inversions is a number that indicates how close or far the given array is from being sorted. For the sorted array, the inversion count...
16 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India