HackerRank: Staircase Solution using PHP (Algorithm)

homezonic

Akande Joshua

Posted on April 16, 2023

HackerRank: Staircase Solution using PHP (Algorithm)

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

💖 💪 🙅 🚩
homezonic
Akande Joshua

Posted on April 16, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related