Skip to content
Programming101
Programmingoneonone

Learn everything about programming

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

Learn everything about programming

HackerRank staircase problem solution

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
Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes