In this “Print Pyramid Pattern” – We have written Java programs to print/draw Pyramid pattern by using different types of Java loops and you can execute and try these Java program through your IDE. This is most [100+] Frequently Asked Java Program on beginner level to check you Java Programming skills and Concept.
Pyramid Pattern Example

This post covers following ways to print Pyramid Pattern:
- Print Pyramid Pattern Java Program: Using for Loop
- Print Pyramid Pattern Java Program: Using While Loop
- Print Pyramid Pattern Java Program: Using Do-While Loop
Pyramid Pattern Java Program: For Loop
This Pyramid Pattern Java Program is following the below logic:
- This program will have three for loop : 1 outer for loop and 2 inner for loop
- Outer for loop is for number of rows or size of Pyramid.
- First inner for loop is spacing from left
- Second inner for loop is for printing the symbols, just double of spacing.
package _1_patterns.pyramid;
import java.util.Scanner;
public class PyramidPatternForLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();
System.out.print("Enter Symbol : ");
char symbol = sc.next().charAt(0);
drawPyramidPattern(size, symbol);
// Close Scanner
sc.close();
}
private static void drawPyramidPattern(int n, char c) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n - i; j++)
{
System.out.print(" ");
}
for (int j = 0; j < (i * 2) - 1; j++)
{
System.out.print(c);
}
System.out.println();
}
}
}
Output
Enter Pattern Size :
8
Enter Symbol : #
#
###
#####
#######
#########
###########
#############
###############
Pyramid Pattern Java Program: While Loop
This Pyramid Pattern Java Program is following the below logic:
- This program will have three while loop : 1 outer while loop and 2 inner while loop
- Outer while loop is for number of rows or size of Pyramid.
- First inner while loop is spacing from left
- Second inner while loop is for printing the symbols, just double of spacing.
package _1_patterns.pyramid;
import java.util.Scanner;
public class PyramidPatternForLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();
System.out.print("Enter Symbol : ");
char symbol = sc.next().charAt(0);
drawPyramidPattern(size, symbol);
// Close Scanner
sc.close();
}
private static void drawPyramidPattern(int n, char c) {
int i = 1, j;
while (i <= n) {
j = 0;
while (j++ < n - i) {
System.out.print(" ");
}
j = 0;
while (j++ < (i * 2) - 1) {
System.out.print(c);
}
System.out.println();
i++;
}
}
}
Output
Enter Pattern Size :
7
Enter Symbol : ^
^
^^^
^^^^^
^^^^^^^
^^^^^^^^^
^^^^^^^^^^^
^^^^^^^^^^^^^
Pyramid Pattern Java Program: Do-While Loop
This Pyramid Pattern Java Program is following the below logic:
- This program will have three do-while loop : 1 outer do-while loop and 2 inner do-while loop
- Outer do-while loop is for number of rows or size of pyramid.
- First inner do-while loop is spacing from left
- Second inner do-while loop is for printing the symbols, just double of spacing.
package _1_patterns.pyramid;
import java.util.Scanner;
public class PyramidPatternForLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();
System.out.print("Enter Symbol : ");
char symbol = sc.next().charAt(0);
drawPyramidPattern(size, symbol);
// Close Scanner
sc.close();
}
private static void drawPyramidPattern(int n, char c) {
int i = 1, j;
do {
j = 0;
do {
System.out.print(" ");
} while (j++ < (n - i - 1));
j = 0;
do {
System.out.print(c);
} while (j++ < i * 2 - 2);
System.out.println();
} while (++i < n);
}
}
Output
Enter Pattern Size :
9
Enter Symbol : %
%
%%%
%%%%%
%%%%%%%
%%%%%%%%%
%%%%%%%%%%%
%%%%%%%%%%%%%
%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
Related Posts
Your Feedback Motivate Us
If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.
Happy Learning !!!
You must log in to post a comment.