Staircase - HackerRank Solution - HackerRank Solutions

Breaking

Post Top Ad

Post Top Ad

Wednesday, July 1, 2020

Staircase - HackerRank Solution

Staircase detail
This is a staircase of size n=4:
   #
  ##
 ###
####

Its base and height are both equal to n. It 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.

Function Description
Complete the staircase function in the editor below.
staircase has the following parameter(s):
  • int n: an integer
Print
Print a staircase as described above.

Input Format
A single integer, n, denoting the size of the staircase.

Constraints
0<n≤100
 .
Output Format
Print a staircase of size n using # symbols and spaces.
Note: The last line must have 0 spaces in it.

Sample Input

Sample Output
     #
    ##
   ###
  ####
 #####
######

Explanation
The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of n=6.


Staircase - HackerRank Solution

import java.util.*;
public class Solution {
    // Complete the staircase function below.
    static void staircase(int n) {
        String symbl = "#";
        for (int i = 0; i < n; i++) {
            System.out.printf("%" + n + "s\n", symbl);
            symbl += "#";
        }
    }
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        staircase(n);
        scanner.close();
    }
}

No comments:

Post a Comment

Post Top Ad