Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Staircase Problem Solution

HackerRank Staircase Problem Solution

Posted on April 10, 2023April 11, 2023 By Yashwant Parihar No Comments on HackerRank Staircase Problem Solution

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

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.

HackerRank Staircase Problem Solution
HackerRank Staircase Problem Solution

Table of Contents

  • Staircase C Solution
  • Staircase C++ Solution
  • Staircase C Sharp Solution
  • Staircase Java Solution
  • Staircase JavaScript Solution
  • Staircase Python Solution

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

  • HackerRank Mini-Max Sum Problem Solution
  • HackerRank Birthday Cake Candles Solution
c, C#, C++, HackerRank Solutions, java, javascript, python Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Plus Minus Problem Solution
Next Post: HackerRank Mini-Max Sum Problem Solution

Related Posts

HackerRank Breadth First Search: Shortest Reach Problem Solution HackerRank Breadth First Search: Shortest Reach c
HackerRank Jim and his LAN Party Problem Solution HackerRank Jim and his LAN Party Solution c
HackerRank Strange Counter Problem Solution HackerRank Strange Counter Problem Solution c
HackerRank Letter Islands Problem Solution HackerRank Letter Islands Problem Solution c
HackerRank Toll Cost Digits Problem Solution HackerRank Toll Cost Digits Problem Solution c
HackerRank Game of Thrones - I Problem Solution HackerRank Game of Thrones – I Problem Solution c

Leave a Reply Cancel reply

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

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme