HackerRank: Staircase Solution using PHP (Algorithm)
Akande Joshua
Posted on April 16, 2023
Problem
This is a staircase of size n=4:
#
##
###
####
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 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;
}
Link to the HackerRank: Staircase Problem
💖 💪 🙅 🚩
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.