New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Hash Staircase Pattern (HackerRank Staircase)

The hash staircase is HackerRank’s “Staircase” problem: print a right-aligned staircase of # symbols, n steps high, with its base and height both equal to n.

Quick answer: Row i prints n − i spaces followed by i hash symbols. The two counts add up to n, so every row ends in the same column and the steps climb to the right. For n = 6 the first row has five spaces and one hash, and the last row is six hashes.

This program is part of the Symbol Patterns collection.

Table of contents8 sections
  1. Hash Staircase Pattern output
  2. How the hash staircase pattern works
  3. Hash Staircase Pattern in C
  4. Hash Staircase Pattern in C++
  5. Hash Staircase Pattern in Java
  6. Hash Staircase Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

Hash Staircase Pattern output

Output (n = 6):

     #
    ##
   ###
  ####
 #####
######
Hash Staircase Pattern layout diagram, row by row (n = 6)
How each row of the hash staircase pattern is built (n = 6). Blue cells are printed characters; grey dots are spaces.

How the hash staircase pattern works

The staircase is a right-aligned triangle. The trick that trips up many first attempts is that the padding must be printed before the symbols, and that it shrinks while the symbols grow. Printing exactly n characters per row keeps the right edge straight, which is what the problem’s checker looks for.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print n − i spaces.
  3. Print i # symbols.
  4. Move to the next line.

Key takeaways

  • Each row is n − i spaces plus i hashes: n characters.
  • The right edge is straight because every row has the same length.
  • In Python, " " * (n − i) + "#" * i builds a row in one line.
  • It is the hash version of the mirrored right triangle.

Hash Staircase Pattern in C

#include <stdio.h>

int main(void) {
    int n = 6;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int j = 1; j <= i; j++)
            printf("#");
        printf("\n");
    }
    return 0;
}

Hash Staircase Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 6;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= i; j++)
            cout << "#";
        cout << endl;
    }
    return 0;
}

Hash Staircase Pattern in Java

public class HashStaircase {
    public static void main(String[] args) {
        int n = 6;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= i; j++)
                System.out.print("#");
            System.out.println();
        }
    }
}

Hash Staircase Pattern in Python

n = 6

for i in range(1, n + 1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, i + 1):
        print("#", end="")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

Why does HackerRank reject my output?

Usually because of trailing spaces or a missing new line. Print exactly n − i spaces and i hashes, then a single new line, with nothing after the last row.

Can I build each row without an inner loop?

Yes. In Python use " " * (n − i) + "#" * i; in Java use " ".repeat(n − i) and "#".repeat(i); in C++ use string(n − i, ' '). The output is the same.

How do I make the staircase go down to the right?

Print i − 1 spaces and n − i + 1 hashes: the steps then start wide on the left and drop towards the right.

How do I let the user choose n?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 17 of 17 in Symbol Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.