How to Print Pyramid of Stars in Java
- Print Pyramid of Stars in Java
- Print Inverted Pyramid of Stars in Java
- Print Double Pyramid of Stars Original and Inverted Together in Java
- Print Half Pyramid of Stars in Java
- Print Inverted Half Pyramid of Stars in Java
![How to Print Pyramid of Stars in Java](/img/Java/feature-image---java-pyramid-of-stars.webp)
Pattern-based exercises are a great way to learn the nested loop in Java, and the print pyramid structure is the best example to do these exercises.
This tutorial demonstrates how to print different pyramids of stars in Java.
Print Pyramid of Stars in Java
Follow the steps below to print the pyramid of stars in Java.
-
Decide the number of rows.
-
The first iteration of the outer loop will print the first row of the pyramid pattern.
-
There will be two loops inside the outer loops: the first loop to print the spaces for each row and the other loop to print the stars for each row of the pyramid.
Let’s implement these steps:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int pyramid_spaces;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// This loop will print one row in every iteration
for (x = 0; x < pyramid_rows; x++) {
// this loop will print the spaces
for (pyramid_spaces = x; pyramid_spaces < pyramid_rows; pyramid_spaces++) {
System.out.print(" ");
}
// this loop will print the stars
for (star_count = 0; star_count < (x + 1); star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
The code above will ask the user to enter the number of rows of the pyramid and print the rows accordingly.
Output:
Enter the number of rows of pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Print Inverted Pyramid of Stars in Java
Now, let’s try to print an inverted pyramid of stars. We can print the inverted pyramid of stars by inverting the process described in the code above.
Example:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int pyramid_spaces;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// This loop will print one row in every iteration
for (x = 0; x < pyramid_rows; x++) {
for (pyramid_spaces = 0; pyramid_spaces < x; pyramid_spaces++) {
System.out.print(" ");
}
for (star_count = x; star_count < pyramid_rows; star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
This code will print the inverted pyramid of stars by taking the input rows from the user.
Output:
Enter the number of rows of pyramid: 7
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Print Double Pyramid of Stars Original and Inverted Together in Java
The double pyramid of stars will create a shape like a diamond.
Example:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int pyramid_spaces;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// Loops for the first pyramid
for (x = 0; x < pyramid_rows; x++) {
for (pyramid_spaces = x; pyramid_spaces < pyramid_rows; pyramid_spaces++) {
System.out.print(" ");
}
for (star_count = 0; star_count < (x + 1); star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
// Loops for the inverted pyramid
for (x = pyramid_rows; x > 0; x = (x - 1)) {
for (pyramid_spaces = pyramid_rows; pyramid_spaces >= (x - 1); pyramid_spaces--) {
System.out.print(" ");
}
for (star_count = (x - 1); star_count > 0; star_count--) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
Output:
Enter the number of rows of pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Print Half Pyramid of Stars in Java
To print the half pyramid, we only require two loops.
Example:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// This loop will print one row in every iteration
for (x = 0; x < pyramid_rows; x++) {
for (star_count = 0; star_count <= x; star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
Output:
Enter the number of rows of pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Print Inverted Half Pyramid of Stars in Java
By changing the second loop in the code above, we can print the inverted half pyramid of stars in Java.
Example:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// This loop will print one row in every iteration
for (x = 0; x < pyramid_rows; x++) {
for (star_count = x; star_count < pyramid_rows; star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
Output:
Enter the number of rows of pyramid: 7
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook