
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Print Spiral Pattern of Numbers
In this article, we will understand how to print spiral pattern of numbers. The pattern is formed by using multiple for-loops and print statements.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter the size : 5
Output
The desired output would be −
The spiral pattern 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 3 2 2 4 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
Algorithm
Step 1 - START Step 2 - Declare three integer values namely i, my_pattern_size and my_input Step 3 - Read the required values from the user/ define the values Step 4 - Assign 2 * my_input – 1 value to my_pattern_size Step 4 - We iterate through two nested 'for' loops to get space between the characters. Step 5 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character. Step 6 - Now, print a value of Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1 + in the subsequent lines. Step 7 - Display the result Step 8 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
import java.util.Scanner; import java.lang.Math; public class SpiralPattern{ public static void main(String args[]){ int my_input , i, j,my_pattern_size ; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the size : "); my_input = my_scanner.nextInt(); System.out.print("The spiral pattern "); System.out.println(); my_pattern_size = 2 * my_input - 1; for( i = 1; i <= my_pattern_size; i++){ for( j = 1; j <= my_pattern_size; j++){ System.out.print(Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1 + " "); } System.out.println(); } } }
Output
Required packages have been imported A reader object has been defined Enter the size : 5 The spiral pattern 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
import java.lang.Math; public class SpiralPattern{ public static void main(String args[]){ int my_input , i, j,my_pattern_size ; System.out.println("Required packages have been imported"); my_input = 5; System.out.println("The size is defined as " +my_input); System.out.print("The spiral pattern "); System.out.println(); my_pattern_size = 2 * my_input - 1; for( i = 1; i <= my_pattern_size; i++){ for( j = 1; j <= my_pattern_size; j++){ System.out.print(Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1 + " "); } System.out.println(); } } }
Output
Required packages have been imported The size is defined as 5 The spiral pattern 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
- Related Articles
- Golang Program to Print Spiral Pattern of Numbers
- Swift program to print spiral pattern
- C program to represent the numbers in spiral pattern
- Program to print a pattern of numbers in C++
- Swift Program to Print Left Triangle Pattern of Numbers
- Java program to print a given matrix in Spiral Form.
- Java Program to Print Diamond Star Pattern
- Java Program to Print Square Star Pattern
- Java Program to Print 8 Star Pattern
- Java Program to Print Inverted Star Pattern
- Java Program to Print X Star Pattern
- Java Program to Print Pyramid Star Pattern
- C program to represent the alphabets in spiral pattern
- Java program to print a given pattern. (stars)
- Java Program to Print Upper Star Triangle Pattern

Advertisements