In this tutorial, we will discuss Java code to Hollow Pyramid Pattern in Java language
In this topic, we will learn how to create te hollow pyramid star pattern in Java programming language
Program 1
This program allows the user to enter the number of rows then it will display hollow pyramid star pattern using for loop in Java language
import java.util.Scanner; class Hollow_Pyramid1{ public static void main (String args[]){ Scanner sc=new Scanner(System.in); System.out.print("Enter the rows: "); int rows=sc.nextInt(); //get input from user for(int i=1; i<=rows; i++){//Print each row for(int j=i; j<rows; j++){//Print space for Pyramid shape System.out.print(" "); } for(int k=1; k<2*i; k++){//print * if(i==rows || (k==1 || k==2*i-1)) System.out.print("*"); else { System.out.print(" "); } } System.out.println(""); //move to next line } } }
When the above code is executed, it produces the following results:
Program 2
This program allows the user to enter the number of rows then it will display hollow inverted pyramid star pattern using for loop in Java language
import java.util.Scanner; class Hollow_Pyramid2{ public static void main (String args[]){ Scanner sc=new Scanner(System.in); System.out.print("Enter the rows: "); int rows=sc.nextInt(); //get input from user for(int i=rows; i>=1; i--){//Print each row for(int j=rows; j>i; j--){//Print space for Pyramid shape System.out.print(" "); } for(int k=1; k<2*i; k++){//print * if(i==rows || (k==1 || k==2*i-1)) System.out.print("*"); else { System.out.print(" "); } } System.out.println(""); //move to next line } } }
When the above code is executed, it produces the following results:
Suggested for you
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language
If statements in Java language
Nested if statements in Java language
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
Floyd’s triangle pattern using nested while loop in Java
Hollow pyramid triangle pattern in C++ language
Rhombus pattern in Java using for loop
Rhombus pattern in C using while loop
Rhombus pattern in C++ using do-while loop
Display Pyramid star pattern in C
Display Pyramid star pattern in Java
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…