Table of Contents
Java program to create an Inverted Pyramid star pattern
In this article, we will discuss the Java program to create an Inverted Pyramid star pattern – using loops
In this post, we will learn how to create an Inverted Pyramid star pattern using for, while and do-wile loop in Java language
Program to Inverted Pyramid using for loop
Program 1
This program allows the user to enter the number of rows and the symbol then the program displays a full inverted pyramid star pattern with the given symbol using for loop in java language
import java.util.*; class ReverseStarPyramid{ public static void main(String args[]){ int i,j,rows;//declare integer variable char ch;//declare character variable Scanner s=new Scanner(System.in);//create scanner object System.out.println("Enter value for row: "); rows=s.nextInt();//read number input from user System.out.println("Enter symbol as you wish: "); ch=s.next().charAt(0);//read number input from user for(i=rows; i>0; i--){//outer for loop for(j=0; j<rows-i; j++){//inner for loop System.out.print(" ");//print star } for(j=0; j<(i*2)-1; j++){//inner for loop 2 System.out.print(ch);//print star } System.out.println(" ");//move to next line } } }
When the above code is executed, it produces the following result
Program to Inverted Pyramid using while loop
Program 2
This program allows the user to enter the number of rows and the symbol then the program displays a full inverted pyramid star pattern with the given symbol using while loop in java language
import java.util.*; class ReverseStarPyramid1{ public static void main(String args[]){ int i,j,rows;//declare integer variable char ch;//declare character variable Scanner s=new Scanner(System.in); //create scanner object for input System.out.println("Enter value for row: "); rows=s.nextInt();//Read input for rows from the user System.out.println("Enter symbol as you wish: "); ch=s.next().charAt(0);Read input for symbol from the user i=rows; while( i>0){ j=0; while(j<rows-i){ System.out.print(" "); j++; } j=0; while(j<(i*2)-1){ System.out.print(ch); j++; } System.out.println(" "); i--; } } }
When the above code is executed, it produces the following result
Program to Inverted Pyramid using do-while loop
Program 3
This program allows the user to enter the number of rows and the symbol then the program displays a full inverted pyramid star pattern with the given symbol using the do-while loop in java language
import java.util.*; class ReverseStarPyramid2{ public static void main(String args[]){ int i,j,rows;//declare integer variable char ch;//declare character variable Scanner s=new Scanner(System.in); //create scanner object for input System.out.println("Enter value for row: "); rows=s.nextInt();//Read input from user as number System.out.println("Enter symbol as you wish: "); ch=s.next().charAt(0);Read input for symbol from the user i=rows; do{//outer do while loop for rows j=0; do{//inner do while loop to space System.out.print(" ");//print space j++; }while(j<=rows-i); j=0; do{ System.out.print(ch);//print star j++; }while(j<(i*2)-1); System.out.println(" "); i--; }while( i>0); } }
When the above code is executed, it produces the following result
Suggested post you
Similar post
C pyramid star pattern program – using loops
C++ pyramid star pattern program – using loops
Java pyramid star pattern program – using loops
C program to create an Inverted Pyramid star pattern
C++ program to create an Inverted Pyramid star pattern