Inverted pyramid pattern

Print hollow half inverted pyramid pattern using ‘*’. See example for more details.

Input Format

First and only line of input contains a single integer N – the size of the pyramid.

Constraints

1 <= N <= 50

Output Format

For the given integer, print hollow half inverted pyramid pattern.

InputcopyOutputcopy
5* * * * *
* *
* *
* *
*
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N - i; j++) {

                if (i == 0 || j == 0 || j == N - i - 1) {
                    System.out.print('*');
                } else {
                    System.out.print(' ');
                }
                if (j < N - i - 1) {
                    System.out.print(' ');
                }
            }
            System.out.println();
        }
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top