Rectangle Example: Write a code fragment that prints a length by height
rectangle of *'s.
for (int row = 0; row < height; row++) {
for (int col = 0; col < length - 1; col++)
System.out.print("*");
System.out.println("*");
}
for (int row = 0; row < height; row++) {
for (int col = 0; col < length ; col++)
System.out.print("*");
System.out.println();
}
Triangle Example:
Write a code fragment that prints a length by length
right triangle of *'s.
for (int row = 0; row < length; row++) {
for (int col = 0; col < row; col++)
System.out.print("*");
System.out.println("*");
}
Diamond Example:
Write a code fragment that prints a diamond of *'s that is n
wide at its widest.
// print the upper pyramid
for (int row = 1; row <= n; row++) {
for (int numSpaces = 0; numSpaces < n - row; numSpaces++)
System.out.print(" ");
for (int numStars = 0; numStars < row; numStars++)
System.out.print("* ");
System.out.println();
}
// print the lower pyramid
for (int row = n - 1; row > 0; row--) {
for (int numSpaces = 0; numSpaces < n - row; numSpaces++)
System.out.print(" ");
for (int numStars = 0; numStars < row; numStars++)
System.out.print("* ");
System.out.println();
}