Hi,

In today’s post, we discuss about printing X pattern.  Already we know how to use looping statements in Java.  Here I am using while – nested while looping statements to derive X pattern.  The expected output is

X Pattern

X Pattern Program

public class XPattern {
public static void main(String[] args) {
int i=0,j;
while(i<3){
j=0;
while(j<(i*1)){
System.out.print(” “);
j++;
}
int k=0;
while(k<=(i-i)){
System.out.print(“*”);
k++;
}
k=0;
while(k<(4-(2*i))){
System.out.print(” “);
k++;
}
k=0;
while(k<=(i*0)){
System.out.print(“*”);
k++;
}
System.out.println();
i++;

}
i=1;
while(i>=0){
j=0;
while(j<(i*1)){
System.out.print(” “);
j++;
}
int k=0;
while(k<=(i*0)){
System.out.print(“*”);
k++;
}
k=4;

while(k>(i*2)){
System.out.print(” “);
k–;
}
j=0;
while(j<=(i*0)){
System.out.print(“*”);
j++;
}

System.out.println();
i–;
}
}}

Output:

X Pattern