Hello coders, today we are going to solve Staircase HackerRank Solution which is a Part of Problem Solving Series.
Task
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 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
6
Sample Output
#
##
###
####
#####
######
Explanation
The staircase is right-aligned, composed of #
symbols and spaces, and has a height and width of n = 6.
Solution – Staircase
C++
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); /* * Complete the 'staircase' function below. * * The function accepts INTEGER n as parameter. */ void staircase(int n) { for(int i=0;i<n;i++) { cout << setfill(' ') << setw(n-(i+1)) << ""; cout << setfill('#') << setw(i+1) << '#'<< endl; } } int main() { string n_temp; getline(cin, n_temp); int n = stoi(ltrim(rtrim(n_temp))); staircase(n); return 0; } string ltrim(const string &str) { string s(str); s.erase( s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))) ); return s; } string rtrim(const string &str) { string s(str); s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end() ); return s; }
Python
#!/bin/python3 import math import os import random import re import sys def staircase(n): # Write your code here for i in range(1, n + 1): print(f'{"#"*i:>{n}}') if __name__ == '__main__': n = int(input().strip()) staircase(n)
Disclaimer: The above Problem (Staircase) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.