Print staircase with both base and height equal to n

Consider a staircase of size n=4, as shown below:

      #
    ##
  ###
####

Observe that its base and height are both equal to n, and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size n.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args) {
 
        int n = 10;
 
        if(n < 1 || n > 100) {
            throw new IllegalArgumentException("N should be between 0 and 100");
        }
 
 
        for (int i = 1; i <= n; i++) {
            System.out.println(Stream.concat(Stream.generate(() -> " ").limit(n-i),
                                             Stream.generate(() -> "#").limit(i))
                                     .collect(Collectors.joining()));
        }
 
    }
}


Comments

Popular posts from this blog

Find the maximum occurring character in given String ?

Find the longest substring without repeating characters