Square Star | Java Patterns

Total Views : 136
Zoom In Zoom Out Read Later Print

In this blog post we will write code in java to print stars in square pattern using 3 different way. At the end of the post, we have added project so that you can download and execute the below codes.

1. Using While Loop.
2. Using Do - While Loop.
3. Using For Loop.

1. Using While Loop

While Loop

package com.manjeet.prodevsblog.stars;

import java.util.Scanner;

public class SquarePattern {
    private static final char DIVIDER = '-' ;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter N : ");
        int n = sc.nextInt();

        System.out.print("Enter Symbol To Use : ");
        char chrToPrint = sc.next().charAt(0);

        printDivider(n);

        int  i = 0;
        while(i++ < n) {

            int  j=0;
            while(j++<n) {
                System.out.print(chrToPrint + " ");
            }
            System.out.println();
        }

    }
    static void printDivider(int n) {
        System.out.println(String.valueOf(DIVIDER).repeat((n * 2) + 3));
    }
}

Output:

Enter N : 
10
Enter Symbol To Use : M
-----------------------
M M M M M M M M M M 
M M M M M M M M M M 
M M M M M M M M M M 
M M M M M M M M M M 
M M M M M M M M M M 
M M M M M M M M M M 
M M M M M M M M M M 
M M M M M M M M M M 
M M M M M M M M M M 
M M M M M M M M M M 

Process finished with exit code 0

1. The outer while loop executes until the condition i++ < n is false in this loop we will check and increment the value same time.

2. The inner loop will display the character until the condition j++ < n is false for every i value as above conditions.

Using Do-While Loop

Do While Loop

package com.manjeet.prodevsblog.stars;

import java.util.Scanner;

public class SquarePattern {
    private static final char DIVIDER = '-' ;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter N : ");
        int n = sc.nextInt();

        System.out.print("Enter Symbol To Use : ");
        char chrToPrint = sc.next().charAt(0);

        printDivider(n);

        int i = 0;
        do {
            int j = 0;
            do {
                System.out.print(chrToPrint + " ");

            } while(++j < n);

            System.out.println();
        } while(++i < n);
    }
    
    static void printDivider(int n) {
        System.out.println(String.valueOf(DIVIDER).repeat((n * 2) + 3));
    }
}

Output:

Enter N : 
8
Enter Symbol To Use : K
-------------------
K K K K K K K K 
K K K K K K K K 
K K K K K K K K 
K K K K K K K K 
K K K K K K K K 
K K K K K K K K 
K K K K K K K K 
K K K K K K K K 

Process finished with exit code 0

1. The outer do-while loop executes until the condition i++ < n is false in this loop we will check and increment the value same time.

2. The inner loop will display the character until the condition j++ < n is false for every i value as above conditions.

Using For Loop

For Loop

package com.manjeet.prodevsblog.stars;

import java.util.Scanner;

public class SquarePattern {
    private static final char DIVIDER = '-' ;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter N : ");
        int n = sc.nextInt();
        System.out.print("Enter Symbol To Use : ");

        char chrToPrint = sc.next().charAt(0);

        printDivider(n);

        for (int i = 0; i < n; i++) {

            for(int j = 0; j < n ; j++) {
                System.out.print(chrToPrint + " ");
            }
            System.out.println();
        }
    }

    static void printDivider(int n) {
        System.out.println(String.valueOf(DIVIDER).repeat((n * 2) + 3));
    }
}

Output:

Enter N : 
12
Enter Symbol To Use : P
---------------------------
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 
P P P P P P P P P P P P 

Process finished with exit code 0

1. Read the n value from the console and store it into the variable n.

2. The outer loop iterates n times with the structure like for(i = 0; i < n; i++).

3. The inner loop iterates n times with the structure for(j = 0; j < n; j++).

4. The inner loop checks the condition j < n for columns to print with provided character, and prints character for n rows and n columns and exit.

You can download the Complete project from below link.

See More

Latest Photos