Top 14 Pyramid Pattern Programs in Java (Print Pyramids)
Pyramid patterns are a fascinating and fundamental aspect of programming that can help you develop a deeper understanding of loops, conditions, and problem-solving in Java. These patterns not only serve as an excellent exercise for honing your coding skills but also have practical applications in various areas of software development.
Whether you're a beginner looking to grasp the basics of programming or an experienced developer aiming to enhance your problem-solving abilities, this tutorial is designed to be your comprehensive guide to creating pyramid patterns in Java.
Why Pyramid Patterns in Java?
Pyramid pattern programs in Java are a captivating way to explore the intricacies of nested loops, which are a core concept in programming. By mastering pyramid patterns, you'll gain valuable insights into loop control, iteration, and logic, all while crafting visually appealing designs.
In this tutorial, we will walk you through a step-by-step journey of creating various pyramid patterns using Java. Starting with the simplest patterns and gradually advancing to more complex ones, you will build a solid foundation in programming logic.
Along the way, you will discover how to:
-
Use Java loops effectively to control the structure of your patterns.
-
Understand the relationship between rows and columns in a pyramid.
-
Customize and modify patterns to suit your creative needs.
-
Apply pyramid patterns to real-world programming problems.
1. Full Pyramid Pattern in Java
Code
Let’s create a full pyramid pattern program in Java:
public class FullPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
*
***
*****
*******
*********
Explanation
In this Java program, we create a Full Pyramid pattern of stars.
-
We define the number of rows in the pyramid using the rows variable. In this example, we have chosen rows = 5.
-
We use two nested loops. The outer loop (for (int i = 1; i <= rows; i++)) controls the rows, and the inner loops handle spaces and stars.
-
The first inner loop (for (int j = 1; j <= rows - i; j++)) prints the required spaces before the stars to create the pyramid shape.
-
The second inner loop (for (int k = 1; k <= 2 * i - 1; k++)) prints the '*' character to form the pyramid.
-
After printing the spaces and stars for each row, we move to the next line using System.out.println() to start the next row.
The result is a Full Pyramid pattern with the specified number of rows. You can adjust the rows variable to create pyramids of different sizes.
2. Reverse Pyramid Pattern in Java
Here's an example of a reverse or Inverted Pyramid pattern in Java:
Code
public class InvertedPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
*********
*******
*****
***
*
Explanation
-
We define the number of rows in the inverted pyramid using the rows variable. In this example, we have chosen rows = 5.
-
We use two nested loops, similar to the Full Pyramid pattern. The outer loop (for (int i = rows; i >= 1; i--)) controls the rows, and the inner loops handle spaces and stars.
-
The first inner loop (for (int j = 1; j <= rows - i; j++)) prints the required spaces before the stars to create the pyramid shape.
-
The second inner loop (for (int k = 1; k <= 2 * i - 1; k++)) prints the '*' character to form the inverted pyramid.
-
After printing the spaces and stars for each row, we move to the next line using System.out.println() to start the next row.
The result is an Inverted Pyramid pattern with the specified number of rows. Adjust the rows variable to create inverted pyramids of different sizes.
3. Hollow Pyramid Pattern in Java
Here's an example of a Hollow Star Pyramid pattern in Java:
Code
public class HollowPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1 || i == rows) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output
*
* *
* *
* *
*********
Explanation
In this Java program, we create a Hollow Pyramid pattern of stars. It consists of '' characters on the edges and spaces in between, creating the hollow effect. The condition if (k == 1 || k == 2 * i - 1 || i == rows) is used to determine when to print '' characters and when to print spaces. Adjust the rows variable to create hollow pyramids of different sizes.
4. Half Pyramid Pattern in Java
Here's an example of a Half Pyramid (Right) pattern in Java:
Code
public class HalfPyramidRight {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
*
**
***
****
*****
Explanation
In this Java program, we create a Half Pyramid (Right) pattern where each row has an increasing number of '*' characters from left to right. Adjust the rows variable to change the height of the pyramid.
5. Number Pyramid Pattern in Java
Here's an example of a Number Pyramid pattern in Java:
Code
public class NumberPyramid {
public static void main(String[] args) {
int rows = 5;
int num = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Explanation
In this Java program, we create a Number Pyramid pattern where each row contains consecutive numbers, increasing from left to right. The num variable keeps track of the current number to be printed. Adjust the rows variable to change the height of the pyramid.
6. Floyd’s Triangle Pattern in Java
Here's an example of a Floyd's Triangle pattern in Java:
Code
public class FloydsTriangle {
public static void main(String[] args) {
int rows = 4;
int num = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
Output
1
2 3
4 5 6
7 8 9 10
Explanation
In this Java program, we create a Floyd's Triangle pattern where consecutive numbers are arranged in a triangular shape. Each row contains one more number than the previous row. The num variable keeps track of the current number to be printed. Adjust the rows variable to change the number of rows in the triangle.
7. Pascal’s Triangle Pattern in Java
Here's a program to print Pascal's Triangle pattern in Java:
Code
public class PascalsTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
int number = 1;
for (int j = 0; j < rows - i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print(" ");
System.out.print(number);
System.out.print(" ");
number = number * (i - k) / (k + 1);
}
System.out.println();
}
}
}
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Explanation
In this Java program, we create a Pascal's Triangle pattern using a mathematical formula to generate the numbers. The numbers in each row are calculated based on the values in the previous row. Adjust the rows variable to change the number of rows in the triangle.
8. Print Diamond Pattern in Java
Here's an example of a Diamond Pattern in Java:
Code
public class DiamondPattern {
public static void main(String[] args) {
int rows = 5;
int spaces = rows - 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= spaces; j++) {
System.out.print(" ");
}
spaces--;
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
spaces = 1;
for (int i = 1; i <= rows - 1; i++) {
for (int j = 1; j <= spaces; j++) {
System.out.print(" ");
}
spaces++;
for (int k = 1; k <= 2 * (rows - i) - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
*
***
*****
*******
*********
*******
*****
***
*
Explanation
In this Java program, we create a Diamond Pattern using a combination of spaces and '*' characters. The pattern is divided into two parts: the upper half and the lower half. Adjust the rows variable to change the size of the diamond pattern.
9. Print Arrowhead Pattern in Java
Here's an example of an Arrowhead Pattern in Java:
Code
public class ArrowheadPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
*
**
***
****
*****
****
***
**
*
Explanation
In this Java program, we create an Arrowhead Pattern by printing rows of '*' characters, first in ascending order and then in descending order. Adjust the rows variable to change the size of the arrowhead pattern.
10. Program to Print Hourglass Pattern in Java
Here's a program to print an Hourglass Pattern in Java:
Code
public class HourglassPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= rows; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= rows; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
*****
****
***
**
*
*
**
***
****
*****
Explanation
In this Java program, we create an Hourglass Pattern by printing rows of '*' characters in a way that resembles an hourglass shape. The pattern is divided into two parts: the upper half and the lower half, which are printed in a top-down and bottom-up manner, respectively.
11. Odd Number Pyramid Program in Java
Below is a Java program to print odd number pyramid:
Code
public class OddNumberPyramid {
public static void main(String[] args) {
int rows = 4;
int currentNumber = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(currentNumber + " ");
currentNumber += 2;
}
System.out.println();
}
}
}
Output
1
3 5
7 9 11
13 15 17 19
Explanation
In this Java program, we create an Odd Number Pyramid where consecutive odd numbers are arranged in each row, starting from 1 and increasing by 2 in each subsequent row. Adjust the rows variable to change the height of the odd number pyramid.
12. 1 22 333 Pyramid Pattern in Java
Now, let’s create a pyramid pattern program in Java to print 1 22 333 and so on pattern.
Code
public class NumberPyramid {
public static void main(String[] args) {
int rows = 3;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
Output
Copy code
1
22
333
Explanation
In this Java program, we create a pyramid pattern with rows containing numbers that repeat in ascending order. The number of repetitions in each row corresponds to the value of that number. Adjust the rows variable to change the height of the pyramid.
13. Hollow Star Pyramid Pattern in Java (Inverted)
Below is a Java program to print inverted hollow pyramid of stars:
Code
public class InvertedHollowPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= rows * 2 - i; k++) {
if (k == i || k == rows * 2 - i || i == rows) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output
*********
* *
* *
* *
*
Explanation
In this Java program, we create an inverted hollow pyramid star pattern. The '*' characters are printed in such a way that they form a hollow pyramid shape. Adjust the rows variable to change the height of the inverted hollow pyramid pattern.
14. Java Program to Print Hollow Star Pyramid
Here's a Java program to create a hollow star pyramid pattern:
Code
public class HollowStarPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1 || i == rows) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output
*
* *
* *
* *
*********
Explanation
In this Java program, we create a hollow star pyramid pattern by printing '' characters and spaces in a way that forms a pyramid shape. The condition within the inner loop controls when to print '' characters and when to print spaces to create the hollow effect. Adjust the rows variable to change the height of the hollow star pyramid pattern.