HackerRank: Staircase Solution using PHP (Algorithm)
Akande Joshua

Akande Joshua @homezonic

About: Experienced Full Stack Developer with expertise in Laravel, Vue.js, Wordpress, HTML, CSS, JavaScript, PHP and MySQL.. My articles here will always be precise and short to the point, that's my style.

Location:
Lagos, Nigeria
Joined:
May 7, 2021

HackerRank: Staircase Solution using PHP (Algorithm)

Publish Date: Apr 16 '23
0 0

Problem

This is a staircase of size n=4:

     #
   ##
  ###
####
Enter fullscreen mode Exit fullscreen mode

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.

Solution (Based on how i solved it)

    $space = " ";
    $hash = "#";

    for ($i = 1; $i <= $n; $i++){
        $res = str_repeat($space, $n - $i) . str_repeat($hash,$i);
echo $res. PHP_EOL;

    }
Enter fullscreen mode Exit fullscreen mode

Link to the HackerRank: Staircase Problem

Comments 0 total

    Add comment