Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Staircase problem solution

YASH PAL, 31 July 20242 May 2025

In this HackerRank Staircase problem 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.

Hackerrank staircase problem solution
Staircase problem solution

HackerRank staircase 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);
});

Other problem solutions –

  • HackerRank Mini-Max sum problem solution
  • HackerRank Birthday cake candles problem solution
  • HackerRank Time conversion problem solution
  • HackerRank Grading students problem solution
  • HackerRank Apple and orange problem solution
algorithm coding problems AlgorithmsHackerRank

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes