Angle Bracket <> in Java with Examples Different types of Recursions in Java Java Lambda Filter Example Java Program for Shopping Bill Pythagorean Triplet with Given Sum Using Single Loop in Java TemporalAdjusters lastInMonth() method in Java with Examples ZIP API in Java Atomic reference in Java Digit Extraction in Java DRY (Don't Repeat Yourself) Principles in Java with Examples Empty array in Java Is main method compulsory in Java? Java I/O Operation - Wrapper Class vs Primitive Class Variables Java Program to Find the Perimeter of a Rectangle Java Thread Priority Java Type Erasure Need of Concurrent Collections in Java Nested ArrayList in Java Print Odd and Even Numbers by Two Threads in Java Square pattern in Java TemporalAdjusters next() method in Java with Examples What does start() function do in multithreading in Java Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples JDBC MySQL Localhost 3306 Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java JDBC MySQL Localhost 3306 Adding a Character as Thousands Separator to Given Number in Java Circular Primes in Java Equilibrium Index Problem in Java Java String regionMatches() Method with Examples LFU Cache Problem in Java Longest Repeating and Non-Overlapping Substring in Java Prefix Suffix Search in Java Product Array Puzzle Problem in Java Russian Doll Envelopes Problem in Java Second Most Repeated Word in a Sequence in Java Special Two-Digit Numbers in a Binary Search Tree in Java Swap Corner Words and Reverse Middle Characters in Java Toggle K bits Problem in Java Upside Down Binary Tree in Java Verbal Arithmetic Puzzle Problem in Java Insert a String into another String in Java Print Shortest Path to Print a String on Screen in Java Search Insert Position in Java BST Sequences in Java Burrows - Wheeler Data Transform Algorithm in Java Convert BST to Min Heap in Java Fibonacci Sum in Java Get name of current method being executed in Java Keith number in Java Longest Even Length Substring in Java Saint-Exupery Numbers in Java Standard practice for protecting sensitive data in Java application Strobogrammatic number in Java Types of Methods in Java Programming Valid Number Problem in Java Boggle Search Problem in Java Convert Java Object to JSON String using Jackson API Generate Random String in Java Java Program to Determine the Unicode Code Point at Given Index in Strin Java 18 snippet tags Jumping Numbers in Java Junction Numbers in Java Find Four Elements that Sums to a given value in Java How to print string vertically in Java How to remove in string in Java Three-Way Partition Problem in Java Apocalyptic Number in Java Check if the given two matrices are mirror images of one another in Java Duplicate Characters Problem in Java Duplicate Parenthesis Problem in Java Sum of Minimum and Maximum Elements of all Subarrays of Size K in Java Triple Shift Operator in Java Valid Pairing of Numbers in Java Valid Sudoku problem in Java Java Cipher Class Kth Missing Positive Number in Java Largest Square Matrix Problem in Java Length of the longest substring without repeating characters in Java Minimum Cost to Make String Valid Problem in Java Ordered pair in Java Range Addition Problem in Java

Square pattern in Java

In this article, we will create a Java program displaying the square pattern. The square pattern can be implemented using loops and print statements. To make the square pattern, you can select any symbol or character you choose, such as *, #, etc.

Before implementing the square pattern, ensure you are familiar with loops in Java.

Example

Input:

N = 6

Output:

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

Using for loop

In this approach, we will display the square pattern using the nested for loop. The algorithm to display the square pattern is given below.

Algorithm

  1. Declare 3 integers variables named i, j and size.
  2. Take the input for the size variable, which will define the square's number of rows and columns.
  3. Create two nested for loops. Both the loops will iterate from 1 to size.
  4. Print the stars for all the columns of the first row.
  5. After printing the first row, print the new line after the inner for loop.

FileName: SquarePattern.java

// Java program to display square pattern using nested for loop

import java.util.*;

public class SquarePattern {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int i;

        int j;

        System.out.print("Enter the size for rows and columns of square: ");

        // take the input

        int size = scanner.nextInt();

        // Outer loop used for rows

        for(i = 1; i <= size; i++) {

            // Nested loop used for columns

            for (j = 1; j<= size; j++) {

                System.out.print("* ");

            }

            // Print the new line

            System.out.println();

        }

    }

}

Output:

Enter the size for rows and columns of square: 6

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

Using recursion

In this approach, we will display the square pattern using recursion

FileName: SquarePattern1.java

// Java program to display square pattern using recursion

import java.util.*;

public class SquarePattern1 {

    // method to print the rows

    private static void row(int i, int n) {

        if(i <= n) {

            column(1, n);

            //Print the new line

            System.out.println();

            row(i+1, n);

        }

    }

   // method to print the columns

    private static void column(int j, int n) {

        if (j <= n) {

            System.out.print("* ");

            column(j + 1, n);

        }

    }

   // main method

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size for rows and columns of square: ");

        // take the input

        int size = scanner.nextInt();

        int i = 1;

        // function call

        row(i, size);

    }

}

Output:

Enter the size for rows and columns of square: 6

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *