# Staircase | HackerRank Solution

Hello coders, today we are going to solve Staircase HackerRank Solution which is a Part of Problem Solving Series.

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.

• 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):