pyramid triangle

Hollow Pyramid Pattern in Java using nested while

Hollow Pyramid Pattern in Java using nested while

In this tutorial, we will discuss Hollow Pyramid Pattern in Java using nested while loop

In this program, we are going to learn about how to display  Pyramid Pattern in Java using nested while loop in Java programming language

Here, we display an Hollow Pyramid Pattern program with coding using nested while loop and also program get input from the user using Scanner class function in Java  language

The user can provide numbers as they wish and get the  Hollow pyramid pattern according to their input.

Hollow Pyuramid Triangle Pattern 1

Program 1

Hollow Pyramid triangle star pattern

import java.util.Scanner;//import the scanner class 
class Pyramid_WhileH1{ 
public static void main(String args[]){ 
Scanner scan=new Scanner(System.in);
//create a scanner object 
System.out.print("Enter the number of rows"); 
int rows=scan.nextInt();

int i,j,k;
    i=1;
    while(i<=rows){
        j=i;
        while(j<rows){
        System.out.print(" ");
        j++;
    }

    k=1;
while(k<2*i){
    if(i==rows || (k==1 || k==2*i-1))
        System.out.print("*");
    else
        System.out.print(" ");
    k++;
}
i++;
System.out.print("\n");//Move to the next line for print
    }
}
}

When the above code is executed, it produces the following results

Hollow Pyramid Pattern in Java

 

Hollow Pyuramid Triangle Pattern 2

Program 2

Inverted Hollow Pyramid triangle star pattern

import java.util.Scanner;//import the scanner class 
class Pyramid_WhileH{ 
public static void main(String args[]){ 
Scanner scan=new Scanner(System.in);
//create a scanner object 
System.out.print("Enter the number of rows"); 
int rows=scan.nextInt();

int i,j,k;
    i=rows;
    while(i>=1){
        j=rows;
        while(j>i){
        System.out.print(" ");
        j--;
    }

    k=1;
while(k<2*i){
    if(i==rows || (k==1 || k==2*i-1))
        System.out.print("*");
    else
        System.out.print(" ");
    k++;
}
i--;
System.out.print("\n");//Move to the next line for print
    }
}
}

When the above code is executed, it produces the following results

inverted Hollow Pyramid Pattern

 

Suggested for you

Operator in Java language

While loop in Java language

Nested while loop in Java language

Data type in Java language

 

Similar post

Java program to print combined Pyramid pattern

C program to print combined Pyramid pattern

C++ program to print combined Pyramid pattern

Java Program to pyramid star Pattern

C Program to pyramid star Pattern

C++ Program to pyramid star Pattern

Hollow Triangle star Pattern using while loop in Java
Hollow Pyramid Pattern in Cpp using nested while loop
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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago