class Demo{
public static void main(String [] args)
{
char prnt = 'X';
int i, j, s, nos = 0; //nos controls the spacing.
//1st triangle
for (i = 9; i >= 1; i = i - 2) {
for (s = nos; s >= 1; s--) { //Spacing control
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
if (j == 1 || j == i) { //This hollows the triangle
System.out.print(prnt);
} else {
System.out.print(" ");
}
}
System.out.print("\n");
nos++; // In the upper triangle the space increments by 1.
}
/* Since both the triangles have the same peak point, while printing the second triangle we'll ignore its peak point. Thus i starts from 3 and the nos from 3.*/
nos = 3;
for (i = 3; i <= 9; i = i + 2) {
for (s = nos; s >= 1; s--) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
if (j == 1 || j == i) {
System.out.print( prnt);
} else {
System.out.print(" ");
}
}
System.out.print("\n");
nos--; //The spaces are in a decrementing order.
}
}
}
OUT PUT :
X X
X X
X X
X X
X
X X
X X
X X
X X
No comments:
Post a Comment