Java practice: an exercise with loops — printing shapes with asterisks
Here’s how to print various shapes with asterisks. This is a good way to get familiar with nested loops and conditionals.
Hollow square
/*
hollowSquare(5); *****
* *
* *
* *
******/
public static void hollowSquare(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0 || i == n - 1 || j == n - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
Left bottom triangle
/*leftBottomTriangle(5); *
**
***
****
******/public static void leftBottomTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Right bottom triangle
/*rightBottomTriangle(5); *
**
***
****
******/ public static void rightBottomTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Right top triangle
/*
rightTopTriangle(5); *****
****
***
**
**/
public static void rightTopTriangle(int n) {
for (int i = n; i > 0; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = i; j > 0; j--) {
System.out.print("*");
}
System.out.println();
}
}
Hollow left top triangle
/*hollowLeftTopTriangle(5); *****
* *
* *
**
**/ public static void hollowLeftTopTriangle(int n) {
for (int i = n; i > 0; i--) {
for (int j = i; j > 0; j--) {
if (i == 1 || i == n || j == 1 || j == i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
Hollow right top triangle
/*hollowRightTopTriangle(5); *****
* *
* *
**
**/ public static void hollowRightTopTriangle(int n) {
for (int i = n; i > 0; i--) {
for (int j = n - i; j > 0; j--) {
System.out.print(" ");
}
for (int j = i; j > 0; j--) {
if (i == 1 || i == n || j == 1 || j == i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}/*
Hollow left bottom triangle
/*hollowLeftBottomTriangle(5); *
**
* *
* *
******/ public static void hollowLeftBottomTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (i == 1 || i == n || j == 1 || j == i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
Hollow right bottom triangle
/*hollowRightBottomTriangle(5); *
**
* *
* *
******/ public static void hollowRightBottomTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
if (i == 1 || i == n || j == 1 || j == i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
X
/*xShape(5); * *
* *
*
* *
* **/ public static void xShape(int n) {
//ternary statement for changing size if even
n = n % 2 == 0 ? n += 1 : n; for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j || j + i == (n + 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println();
}
Bow tie
/*bowTie(5); * *
** **
******
** **
* **/ public static void bowTie(int n) {
int x = 1;
int midPoint = n / 2;
if ((n % 2) == 0) midPoint--;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j < x || j >= n − x) System.out.print("*");
else System.out.print(" ");
}
if (i < midPoint) x++;
if (i >= midPoint) x--;
System.out.println();
}
}
Diamond
/*diamond(4); *
***
*****
*******
*********
*******
*****
***
**/ public static void diamond(int n) { for (int i = 0; i < n; i++) {
for (int j = n; j > i; j--) {
System.out.print(" ");
} for (int j = 0; j <= i; j++) {
System.out.print("*");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
} for (int i = 0; i <= n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
} for (int j = n; j > i; j--) {
System.out.print("*");
} for (int j = n; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
}
Feel free to reach out to me for a chat or if you have any questions/comments/suggestions :)