Program Description:
Write a program to print 21 rows of X's in the shape of a large X as illustrated below. Be sure so the two rows intersect at the "11" row.
Here is what I want as an output:
Here is what I have so far.
public class Program168h {
public static void main (String [] args) {
String d= "X";
for (int a = 1; a < 23; a++) {
for (int b = a; b >= 1; b--) {
System.out.print(" ");
}
System.out.print(d);
for (int x = a; x < 22; x++) {
System.out.print(" ");
}
System.out.print(d);
System.out.println();
}
}
}
This only produces the first half of the X, I do not know how to produce the lower half.
You can try that:
Though the above solutions work perfectly, I tried to experiment by not using nested for and the soultion is as below. This will have higher performance than using nested for which has a complexity of O(n2) when compared to O(n) in this.
Try this one:
explanation: The first operate on Xaxis coordinates, second for operates on Yaxis. Our task is to cover diagonal. Covering first diagonal is where coordinateX == coordinateY. In code is if(i==j). These are points (1,1), (2,2)...... Second diagonal are points where (x,y)= (20,1),(19,2),(18,3) .... This situation covers second if(i == ySize - j - 1) .