Staircase HackerRank Solution in C, C++, Java, Python

Staircase detail

This is a staircase of size n=4 :

  #

  ##

 ###

####

Its base and height are both equal to . 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 .

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

Sample Output

    #

    ##

   ###

  ####

 #####

######

Explanation

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of n=6.

 

Staircase HackerRank 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");
    }
   return 0;
}

Staircase HackerRank Solution in C++

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


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

Staircase HackerRank 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) {
        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 HackerRank Solution in Python

a=int(raw_input())
for i in range(1,a+1):
    print (a-i)*" "+i*"#"

Staircase HackerRank Solution in C#

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 */
        var n = int.Parse(Console.ReadLine());
        
        for (var i = 0; i < n; i++)
        {
            for (var j = 0; j < n - 1 - i; j++)
            {
                Console.Write(" ");
            }
            
            for (var j = 0; j < i + 1 ; j++)
            {
                Console.Write("#");
            }
            
            Console.WriteLine();
        }
    }
}

 

Attempt – Staircase HackerRank Challenge

Link – https://www.hackerrank.com/challenges/staircase

Next Challenge – Mini Max Sum HackerRank Solution

Link – https://exploringbits.com/mini-max-sum-hackerrank-solution/

 

Leave a Comment