pyramid triangle

Java code to Hollow Pyramid Pattern

Java code to Hollow Pyramid Pattern

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

Hollow Pyramid pattern

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:

Hollow pyramid pattern 1

 

Hollow inverted Pyramid pattern

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:

Hollow pyramid pattern 2

 

Suggested for you

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

 

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

 

If statements in Java  language

Nested if statements in Java  language

 

Display Pyramid star pattern in C

Display Pyramid star pattern  in Java

Floyd’s triangle pattern using nested while loop in Java

Cpp program to Hollow Pyramid Pattern
Cpp program to display Pyramid star pattern
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago