Write a Java Program to print inverted star pyramid pattern using for loop. This Java inverted pyramid pattern example uses nested for loops to iterate and display the output.
package ShapePrograms; import java.util.Scanner; public class InvertedPyramid1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Inverted Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Inverted Pyramid Star Pattern"); for (int i = rows ; i >= 1; i-- ) { for (int j = 0 ; j < rows - i; j++ ) { System.out.print(" "); } for (int k = 0 ; k != (2 * i) - 1; k++ ) { System.out.print("*"); } System.out.println(); } } }
In this Java inverted star pyramid program, we replaced the for loops with while loops.
package ShapePrograms; import java.util.Scanner; public class InvertedPyramid2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Inverted Pyramid Pattern Rows = "); int rows = sc.nextInt(); int i = rows, j, k ; while ( i >= 1) { j = 0 ; while (j < rows - i ) { System.out.print(" "); j++; } k = 0 ; while ( k != (2 * i) - 1) { System.out.print("*"); k++ ; } System.out.println(); i-- ; } } }
Enter Inverted Pyramid Pattern Rows = 10
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
Java Program to Print Inverted Star Pyramid using do while loop
package ShapePrograms; import java.util.Scanner; public class InvertedPyramid3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Inverted Pyramid Pattern Rows = "); int rows = sc.nextInt(); int i = rows, j, k ; do { j = 0 ; do { System.out.print(" "); } while (j++ < rows - i ); k = 0 ; do { System.out.print("*"); } while ( ++k != (2 * i) - 1); System.out.println(); } while ( --i >= 1) ; } }
Enter Inverted Pyramid Pattern Rows = 13
*************************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
In this Java example, the InvertedPyramidPattern function prints the inverted pyramid pattern of a given symbol.
package ShapePrograms; import java.util.Scanner; public class InvertedPyramid4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Inverted Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Character for Inverted Pyramid Pattern = "); char ch = sc.next().charAt(0); InvertedPyramidPattern(rows, ch); } public static void InvertedPyramidPattern(int rows, char ch) { for (int i = rows ; i >= 1; i-- ) { for (int j = 0 ; j < rows - i; j++ ) { System.out.print(" "); } for (int k = 0 ; k != (2 * i) - 1; k++ ) { System.out.print(ch); } System.out.println(); } } }
Enter Inverted Pyramid Pattern Rows = 16
Character for Inverted Pyramid Pattern = $
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$
$$$$$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$
$$$$$$$
$$$$$
$$$
$