Algorithm


Problem Name: Algorithms - Staircase

Problem Link: https://www.hackerrank.com/challenges/staircase/problem?isFullScreen=true

In this HackerRank Functions in Algorithms - Java programming problem solution,

 

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,

, 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

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();

// Complete the staircase function below.
void staircase(int n) {
    for(int i = 1; i  < = n; i++){
        for(int j = 1; j  < = n; j++){
            if(i + j > n){
                printf("#");
            }
            else{
                printf(" ");
            }
        }
        printf("\n");
    }
}

int main()
{
    char* n_endptr;
    char* n_str = readline();
    int n = strtol(n_str, &n_endptr, 10);

    if (n_endptr == n_str || * n_endptr != '\0') { exit(EXIT_FAILURE); }

    staircase(n);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) { break; }

        data_length += strlen(cursor);

        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);

        if (!data) { break; }

        alloc_length = new_length;
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }

    data = realloc(data, data_length>;

    return data;
}
Copy The Code & Try With Live Editor

#5 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;

// Complete the staircase function below.
void staircase(int n) {

    for(int i=0;i < n;i++) {
        cout << setfill(' ') << setw(n-(i+1)) << "";
        cout << setfill('#') << setw(i+1) << '#'<< endl;
    }
}

int main()
{
    int n;
    cin >> n;
    cin.ignore(numeric_limits < streamsize>::max(), '\n');

    staircase(n);

    return 0;
}
Copy The Code & Try With Live Editor

#3 Code Example with Java Programming

Code - Java Programming


import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        int num  = Integer.parseInt(sc.nextLine());
        for(int j = 0; j  <  num; j++){
            for(int i = 1; i  < = num; i++){
                System.out.print(i < num-j ? " " : "#");
            }
            System.out.println("");
        }
        
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with C# Programming

Code - C# Programming


using System;
using static System.Console;

class Solution
{
    static void Main(String[] args)
    {
        var n = int.Parse(ReadLine());
        for (int i = 1; i  < = n; i++)
        {
            var spaces = new String(' ', n - i);
            var hashes = new String('#', i);
            WriteLine(spaces + hashes);
        }
    }
}
Copy The Code & Try With Live Editor

#5 Code Example with Python Programming

Code - Python Programming


def staircase(n):
    for i in range(1, n + 1):
        print(f'{"#" * i : > {n}}')

if __name__ == '__main__':
    n = int(input())

    staircase(n)
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Plus Minus in Java solution in Hackerrank - Hacerrank solution Java
Next
[Solved] Mini-Max Sum in C, C++,Java, JavaScript, Python, PHP & C# solution in Hackerrank