Search code examples
javacnested-loops

How to create an "X" pattern?


I want to know how to create the following pattern using * and _ using nested loops.

*_______*
__*___* 
____*   
__*___* 
*_______*

I am currently trying to do this in Java, but an answer in C will do as well, I can extrapolate the answer. Here is a pic to better understand the pattern required.

enter image description here

I understand that I need to have some code to start with , but I cant manage to work out the logic.

Is there any algo/technique that anyone can guide me towards in order to be able to attempt this, some Karnaugh map like technique.


Solution

  • for(int i =0; i < 5; i++){
        for(int j = 0; j < 9; j++){
           if(j==2*i || 8-2*i == j)
              System.out.print("*");
           else if(8-2*i < j && j > 2*i)
               break;
           else
              System.out.print("_");
        }
      System.out.println();
    }