6 Pyramid Pattern In Java
In this tutorial, you will look at 6 different types of pyramid patterns in Java with their complete code.
- Star Pyramid Pattern
- Hollow Star Pyramid Pattern
- Reverse Star Pyramid Pattern
- Hollow Reverse Star Pyramid Pattern
- Number Pyramid Pattern
- Alphabet Pyramid Pattern
Table Of Contents
1. Star Pyramid Pattern in Java
* *** ***** ******* *********
Let's start with the simplest pyramid pattern, the star pyramid pattern. In this pattern, we print stars in a pyramid shape.
You can see each row has stars equal to the 2 * row number - 1. For example, the first row has 1 star, the second row has 3 stars, and so on.
- Take the size of the pyramid from the user as input.
- Call the starPyramid() method and pass the size as an argument.
- Inside the starPyramid() method, we have two nested for loops.
- The first for loop prints spaces before the stars, equal to size - row number.
- The second for loop prints the stars, equal to 2 * row number - 1.
- Print a new line after each row.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the pyramid: ");
int size = sc.nextInt();
starPyramid(size);
sc.close();
}
// function to print star pyramid
static void starPyramid(int size) {
// Outer loop for the number of rows
for (int i = 1; i <= size; i++) {
// first inner loop for spaces
for (int j = 1; j <= size - i; j++) {
System.out.print(" ");
}
// second inner loop for stars
for (int k = 1; k <= 2*i -1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Enter the size of the pyramid: 5 * *** ***** ******* *********
2. Hollow Star Pyramid Pattern
* * * * * * * *********
Now, let's look at the hollow star pyramid pattern.
Hollow star pyramid is the same as the star pattern but it has stars only at the boundaries.
- Take the size of the pyramid from the user.
- Call the hollowStarPyramid() method and pass the size as an argument.
- Inside the hollowStarPyramid() method, we have two nested for loops.
- The first for loop prints spaces just like the previous pattern.
- The second for loop is interesting. It prints stars only if it's the first or last column or the last row.
- Print a new line after each row.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the pyramid: ");
int size = sc.nextInt();
hollowStarPyramid(size);
sc.close();
}
// function to print hollow star pyramid
static void hollowStarPyramid(int size) {
// Outer loop for the number of rows
for (int i = 1; i <= size; i++) {
// first inner loop for spaces
for (int j = 1; j <= size - i; j++) {
System.out.print(" ");
}
// second inner loop for stars and intermediate spaces
for (int k = 1; k <= 2*i -1; k++) {
// check if its first and last column or last row
// print star otherwise print space
if(k == 1 || k == 2*i -1 || i == size)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Enter the size of the pyramid: 5 * * * * * * * *********
3. Inverted Star Pyramid Pattern
********* ******* ***** *** *
Inverted star pyramid pattern looks like water image of the normal star pyramid pattern.
Here is the complete code to print the inverted star pyramid pattern.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the pyramid: ");
int size = sc.nextInt();
invertStarPyramid(size);
sc.close();
}
// function to print inverted star pyramid
static void invertStarPyramid(int size) {
// Outer loop for the number of rows
for (int i = 1; i <= size; i++) {
// first inner loop for spaces
for (int j = 1; j <= i - 1; j++) {
System.out.print(" ");
}
// second inner loop print star
for (int k = 1; k <= 2 * (size - i) + 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Enter the size of the pyramid: 5 ********* ******* ***** *** *
4. Inverted Hollow Star Pyramid Pattern
********* * * * * * * *
Inverted hollow star pyramid pattern is the same as the inverted star pyramid pattern except that is hollow inside.
Here is the complete code to print the inverted hollow star pyramid pattern.
import java.util.Scanner;
public class z {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the pyramid: ");
int size = sc.nextInt();
hollowInvertStarPyramid(size);
sc.close();
}
// function to print inverted hollow star pyramid
static void hollowInvertStarPyramid(int size) {
// Outer loop for the number of rows
for (int i = 1; i <= size; i++) {
// first inner loop for spaces
for (int j = 1; j <= i - 1; j++) {
System.out.print(" ");
}
// second inner loop for stars and spaces
for (int k = 1; k <= 2 * (size - i) + 1; k++) {
// print star for first and last column and for first row
if (k == 1 || k == 2 * (size - i) + 1 || i == 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Enter the size of the pyramid: 5 ********* * * * * * * *
5. Number Pyramid Pattern
1 123 12345 1234567 123456789
Above we have seen Pyramid pattern using stars now let's look at Number pyramid pattern.
We have to follow the same logic as we did above but instead of printing star we have to print numbers.
Print numbers from 1 to i in a row and reset the counter to 1 for the next row.
Here is the complete code to print the number pyramid pattern.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the pyramid: ");
int size = sc.nextInt();
numberPyramid(size);
sc.close();
}
static void numberPyramid(int size) {
// Outer loop for the number of rows
for (int i = 1; i <= size; i++) {
// first inner loop for spaces
for (int j = 1; j <= size - i; j++) {
System.out.print(" ");
}
// second inner loop for printing numbers
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print(k);
}
System.out.println();
}
}
}
Enter the size of the pyramid: 5 1 123 12345 1234567 123456789
6. Alphabet Pyramid Pattern
A ABC ABCDE ABCDEFG ABCDEFGHI
Just like the number pattern with a little tweak you can create Alphabet pyramid pattern.
Just add 64 to the counter and typecast it to char to print the alphabet.
Here is the complete code to print the alphabet pyramid pattern.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the pyramid: ");
int size = sc.nextInt();
alphabetPyramid(size);
sc.close();
}
static void alphabetPyramid(int size) {
// Outer loop for the number of rows
for (int i = 1; i <= size; i++) {
// first inner loop for spaces
for (int j = 1; j <= size - i; j++) {
System.out.print(" ");
}
// second inner loop for printing numbers
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print((char) (k + 64));
}
System.out.println();
}
}
}
Enter the size of the pyramid: 5 A ABC ABCDE ABCDEFG ABCDEFGHI