In this post, We are going to solve HackerRank Staircase Problem.
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 a staircase as described above.
Input Format
A single integer, n, denotes 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.
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.

Staircase C Solution
#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 i, k, n;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (k = 0; k < n - i; k++) {
printf(" ");
}
for (k = 0; k < i; k++) {
printf("#");
}
printf("\n");
}
return 0;
}
Staircase C++ Solution
#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;
N++;
for (int i = 1; i < N; i++) {
int j;
for (j = 1; j < N - i; j++) {
cout << ' ';
}
for (int k = 0; k < N - j; k++) {
cout << '#';
}
cout << endl;
}
return 0;
}
Staircase C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int t = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= t; i++) {
String strhash = new String('#',i);
Console.WriteLine( strhash.PadLeft(t) );
}
}
Staircase Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'staircase' function below.
*
* The function accepts INTEGER n as parameter.
*/
public static void staircase(int n) {
var nn = n;
for(int i=1;i<=n;i++) {
System.out.println(" ".repeat(nn-1) + "#".repeat(i));
nn = nn - 1;
}
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim());
Result.staircase(n);
bufferedReader.close();
}
}
Staircase JavaScript Solution
function processData(input) {
var stair = '',
N = +input,
i = 1;
while (++i < N + 2) console.log(new Array(N + 2 - i).join(' ') + new Array(i).join('#'));
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Staircase Python Solution
size = int(input())
for i in range(1,size+1):
print("%s%s" % (" "*(size-i), "#"*i))
Other Solutions