HackerRank Staircase Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Staircase Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Staircase Solution
HackerRank Solve Me First problem solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Staircase Solution

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

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.

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.

HackerRank Staircase Solution

Staircase Solution in C

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

int main() {
    int n, i,j;
    scanf("%d", &n);
    for(i=0;i<n;i++)
        {
        for(j=0;j<n;j++)
            {
            if(j<(n-1-i)) printf(" ");
            else printf("#");
            }
        printf("\n");
    }

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Staircase Solution in Cpp

#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;
    string str(n, ' ');
        
    for (int i = 1; i <= n; ++i) {
        str[n-i] = '#';
        cout << str << endl;
    }
    return 0;
}

Staircase Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    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 sc = new Scanner(System.in);
        
        int i,j,k,n = sc.nextInt();
        
        for(i=0;i<n;i++){
          
            for(j=n-1;j>i;j--){
                System.out.print(" ");
            }
            for(k=0;k<=i;k++)
                System.out.print("#");
           System.out.println(); 
        }
        
    }
}

Staircase Solution in Python

n=int(input())
m=" "
t=1
while n>n-n:
    print((n-1)*m+t*("#"))
    n=n-1
    t=t+1

Staircase Solution using JavaScript

function processData(input) {
  var h = parseInt(input, 10);
  var i;
  var j;
  var line;
  
  for (i = 0; i < h; i++) {
    line = '';
    for (j = 0; j < h - i - 1; j++) {
      line += ' ';
    }
    for (;j < h; 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);
});

Staircase Solution in Scala

object Solution {

    def main(args: Array[String]) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
*/
        val n = readInt
        for (i <- 0 to n - 1) {
            for (j <- i to n - 2) {
                print(" ")
            }
            for (j <- 0 to i) {
                print("#")
            }
            println()
        }
    }
}

Staircase Solution in Pascal

var
    N, k, i, l:integer;

begin
    readln(N);
    l:=1;
    
    
    for k := 1 to N do
    begin
        for i := 1 to N do
        begin
            if i < N - l + 1 then
                write(' ')
            else
                write('#');
        end;
        writeln;
        inc(l);
    end;
end.

Disclaimer: This problem (Staircase) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi


Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment