DEV Community

Akande Joshua
Akande Joshua

Posted on

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay