Here is a java program to print up-side down pyramid pattern of stars. In Reverse pyramid star pattern of N rows, ith row contains (i-1) space characters followed by (2*i - 1) star(*) characters. Check below mentioned inverted pyramid pattern of 4 rows.
Sample Output for 4 rows ******* ***** *** *
Algorithm to print inverted pyramid star pattern using loop
This C program is similar to pyramid star pattern, except here we are printing the rows in reverse order.
This C program is similar to pyramid star pattern, except here we are printing the rows in reverse order.
- We first take the number of rows in the pattern as input from user and store it in an integer variable "rows".
- One iteration of outer for loop will print a row of inverted pyramid.
- For any row i, inner for loop first prints i-1 spaces followed by a while loop which prints (2*i - 1) star character.
Java program to print inverted pyramid star pattern
package com.tcc.java.programs;
import java.util.*;
public class InvertedPyramidPattern {
public static void main(String args[]) {
int rows, i, space, star=0;;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of rows in pattern");
rows = in.nextInt();
for(i = rows;i >= 1; i--) {
// Printing spaces
for(space = 0; space <= rows-i; space++) {
System.out.print(" ");
}
// Printing stars
star = 0;
while(star != (2*i - 1)) {
System.out.print("*");
star++;
}
System.out.print("\n");
}
}
}
Output
Enter number of rows in pattern
6
***********
*********
*******
*****
***
*
Recommended Posts