In this HackerRank Staircase problem, you need to complete the staircase function that has an integer parameter and needs to print the staircase of that size.

HackerRank Staircase problem solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
    for i in range(0,n):
        for j in range(0,n):
            if i + j >= n-1:
                print("#",end='') 
            else:
                print(" ",end='')
        print("\r")

if __name__ == '__main__':
    n = int(input())

    staircase(n)


Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {
    
    public static void drawStair(int size) {
        int level = size-1;
        for(int i = 0; i < size; i++) {
            StringBuilder s = new StringBuilder();
            for(int k = 0; k < level; k++) {
                s.append(" ");
            }
            for(int k = 0; k < size - level; k ++){
                s.append("#");
            }
            level -= 1;
            System.out.println(s.toString());
        }
    }

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        Solution sol = new Solution();
        
        int size = in.nextInt();
        sol.drawStair(size);
    }
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            printf(" ");
        }
        for (int j = n - i -1; j < n; ++j) {
            printf("#");
        }
        printf("\n");
    }
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int height;
    scanf("%d",&height);
    int stair = height - 1;
    
    for (int i = 0; i < height; ++i){
        for (int j = 0; j < height; ++j){
            if (j >= stair){
                printf("#");
            } else{
                printf(" ");
            }
        }
        stair -= 1;
        printf("\n");
    }    
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    
    var steps = parseInt(input);
    
    for (var i = 0; i++ < steps; ) {
        
        var line = '';
        var spaces = steps - i;
        
        for (var j = spaces; j--; ) {
            line += ' ';
        }
        
        for (var j = i; j--; ) {
            line += '#';
        }
        
        console.log(line);
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});