Beginners Programs :

X Star Pattern Java Program – Patterns

Java program to print X star pattern program – We have written the below print/draw X asterisk/star pattern program in four different ways with sample example and output, check it out. At the end of the program, we have added compiler so that you can execute the below codes.

  • Print X Star Pattern – Using For Loop
  • Print – Using While Loop
  • print – Using Do While Loop

Using For Loop

1) In the inner for loop iterates from j=1 to k and prints charter if  j=i or j=k-i+1  displays “*”,else it displays space.

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

Output:

Using While Loop

1) While loop checks the condition first then executes the code.

2) First checks the condition at while loop i.e i<=k, if it is true, then it comes to the inner while loop.

3) In inner while loop first checks the condition j<=k, then it executes the code in the loop until the condition is false, then cursor come out of the inner loop and goes to the outer loop, this will continue until the condition i<=k is false.

Output:

Using Do-While Loop

1) First, the code will be executed i.e. inner do-while loop, the code in the inner loop executes until the condition j<=k is false. It prints charter for j=i ,j=k-i+1.Other than these j values prints space.

2) If the condition false then cursor comes to outer do-while loop. The outer do loop execution will stop if the condition i<=k is false

Output:

More Programs: