X Star | Java Patterns

Total Views : 116
Zoom In Zoom Out Read Later Print

In this blog post we will write code in java to print stars in X 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 XPattern {
    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 = 1, j;
        int k = n * 2 - 1;

        while (i <= k) {
            j = 1;
            while (j <= k) {
                if (j == i || j == k - i + 1) {
                    System.out.print(chrToPrint);
                }
                System.out.print(" ");
                j++;
            }
            System.out.println();
            i++;
        }
    }

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

Output:

Enter N : 
8
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 

Process finished with exit code 0

1. While loop checks the condition first and if it is true then it executes the code of the block.

2. if the first condition at while loop is true, then it comes to the inner while loop.

3. In inner while loop first checks the j <= k condition, then it executes the code in the loop until the same condition is false, then cursor continue from the outer loop until it is true and then come back to inner loop with j <= i again else it will follow code after outer while loop.

Using Do-While Loop

Do While Loop

package com.manjeet.prodevsblog.stars;

import java.util.Scanner;

public class XPattern {
    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 = 1, j;
        int k = n*2-1;

        do {
            j = 1;
            do {
                if(j == i || j == k-i+1) {
                    System.out.print(chrToPrint);
                }
                System.out.print(" ");
                j++;

            } while(j <= k) ;

            System.out.println();
            i++;

        } while(i <= k);
    }

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

Output:

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

Process finished with exit code 0

1. First, the code will be executed before checking any condition i.e. inner do-while loop, the code in the inner loop executes until the condition j <= k is false. It prints character we want to print for j=i ,j=k-i+1, other than these j values prints space.

2. If the condition false in inner do-while loop then cursor comes to outer do-while loop. The outer do-while loop execution will stop if the condition i <= k is false and then follow the execution of next code.

Using For Loop

For Loop

package com.manjeet.prodevsblog.stars;

import java.util.Scanner;

public class XPattern {
    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 k = n*2-1;
        for(int i = 1; i <= k; i++) {

            for(int j = 1; j <= k; j++) {

                if(j == i || j == k-i+1) {
                    System.out.print(chrToPrint);
                }
                System.out.print(" ");
            }
            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 : F
-----------------------
F                  F 
 F                F  
  F              F   
   F            F    
    F          F     
     F        F      
      F      F       
       F    F        
        F  F         
         F          
        F  F         
       F    F        
      F      F       
     F        F      
    F          F     
   F            F    
  F              F   
 F                F  
F                  F 

Process finished with exit code 0

1. First, outer loop will be executed if the inputs are valid and go to the inner loop.

2. In the inner for loop iterates from j = 1 to k and prints character entered by user if  j = i or j = k-i+1  displays "*", else it will displays a space.

3. This inner for-loop code will be execute until the inner for loop condition is false, then it comes to the outer for-loop, the outer for-loop will execute until the condition i <= k is false.

You can download the Complete project from below link.

See More

Latest Photos