Java programs to print the half pyramid pattern have been given here. Both the right half and left half of the pyramid using *, numbers and letters have been covered.
Page content(s):
1. Program & Output: Right half pyramid
2. Program & Output: Left half pyramid
3. Program & Output: Inverted half pyramid
1. Program & output to print the right half pyramid pattern
1.1. Java Program & output to print the right half pyramid pattern using *
/***************************** alphabetacoder.com Java program to print the right half pyramid pattern using * ******************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print * for (j = 1; j <= i; j++) { System.out.print("*"); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 6
*
**
***
****
*****
******
1.2. Java Program & output to print the right half pyramid pattern using numbers
/********************************** alphabetacoder.com Java program to print the right half pyramid pattern using numbers ***********************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print numbers for (j = 1; j <= i; j++) { System.out.print(j + " "); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1.3. Java Program & output to print the right half pyramid pattern using alphabets
/*********************************** alphabetacoder.com Java program to print the right half pyramid pattern using alphabets ************************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print letters for (j = 1; j <= i; j++) { System.out.print((char)(j + 64) + " "); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 6
A
A B
A B C
A B C D
A B C D E
A B C D E F
2. Program & output to print the left half pyramid pattern
2.1. Java Program & output to print the left half pyramid pattern using *
/*********************************** alphabetacoder.com Java program to print the left half of the pyramid pattern using * ************************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print space for (j = 1; j <= row - i; j++) { System.out.print(" "); } // print * for (j = 1; j <= i; j++) { System.out.print("*"); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 6
*
**
***
****
*****
******
2.2. Java Program & output to print the left half pyramid pattern using numbers
/*********************************** alphabetacoder.com Java program to print the left half of the pyramid pattern using numbers ************************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print space for (j = 1; j <= 2 * (row - i); j++) { System.out.print(" "); } // print numbers for (j = 1; j <= i; j++) { System.out.print(j + " "); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2.3. Java Program & output to print the left half pyramid pattern using alphabets
/************************************* alphabetacoder.com Java program to print the left half of the pyramid pattern using alphabets **************************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print space for (j = 1; j <= 2 * (row - i); j++) { System.out.print(" "); } // print letters for (j = 1; j <= i; j++) { System.out.print((char)(j + 64) + " "); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 5
A
A B
A B C
A B C D
A B C D E
3. Program & output to print the right half pyramid pattern
3.1. Java Program & output to print the inverted half pyramid pattern using *
/********************************* alphabetacoder.com Java program to print the inverted half pyramid pattern using * **********************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print * for (j = row; j >= i; j--) { System.out.print("*"); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 6
******
*****
****
***
**
*
3.2. Java Program & output to print the inverted half pyramid pattern using numbers
/********************************* alphabetacoder.com Java program to print the inverted half pyramid pattern using numbers **********************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print numbers for (j = row; j >= i; j--) { System.out.print((row - j + 1) + " "); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 6
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
3.3. Java Program & output to print the inverted half pyramid pattern using alphabets
/*********************************** alphabetacoder.com Java program to print the inverted half pyramid pattern using alphabets ************************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int row, i, j; // take input System.out.print("Enter the number of rows: "); row = sc.nextInt(); // new line System.out.println(""); // display the pattern for (i = 1; i <= row; i++) { // print letters for (j = row; j >= i; j--) { System.out.print((char)(row - j + 65) + " "); } // new line System.out.println(""); } } }
Output
Enter the number of rows: 6
A B C D E F
A B C D E
A B C D
A B C
A B
A