Problem source: https://www.hackerrank.com/challenges/staircase

Your teacher has given you the task to draw the structure of a staircase. Being an expert programmer, you decided to make a program for the same. You are given the height of the staircase. You need to print a staircase as shown in the example.

**Input Format**

You are given an integer N depicting the height of the staircase.

**Constraints**

1≤N≤100

**Output Format**

Draw a staircase of height N in the format given below.

For example:

```
#
##
###
####
#####
######
```

Staircase of height 6, note that last line has 0 spaces before it.

**Sample Input**

```
6
```

**Sample Output**

```
#
##
###
####
#####
######
```

**Method 1: (using nested for loops)**

Algorithm:

- Get the number of total rows or maximum number of #s that we need to print from s standard input.
- Loop over total number of rows because we need to print white spaces and # in each line.
- In first iteration or row, we need to print 5 white spaces and then #. At this point T = 6, i = 1. So, we need to print “i” number of #s i.e. # and (T-i) white spaces i.e. 5 spaces.
- In second iteration, we need to print 4 white spaces and ##. At this point T = 6, i = 2. So, we need to print “i” number of #s i.e. ## and (T-i) white spaces i.e. 4 spaces.
- In Tthird iteration, we need to print 3 white spaces and ###. At this point T = 6, i = 3. So, we need to print “i” number of #s i.e. ### and (T-i) white spaces i.e. 3 spaces.
- In forth iteration, we need to print 2 white spaces and ####. At this point T = 6, i = 4. So, we need to print “i” number of #s i.e. #### and (T-i) white spaces i.e. 2 spaces.
- In fifth iteration, we need to print 1 white spaces and #####. At this point T = 6, i = 5. So, we need to print “i” number of #s i.e. ##### and (T-i) white spaces i.e. 1 spaces.
- In sixth iteration, 0 white space and ######. At this point T = 6, i = 6. So, we need to print “i” number of #s i.e. ###### and (T-i) white spaces i.e. 0 spaces.

public static void PrintStairCase() { // standard input for total number of rows or maximum number of #s int T = Convert.ToInt16(Console.ReadLine()); // loop over number of rows for (short i = 1; i <= T; ++i) { for (int j = 1; j <= T; ++j) { if (j <= T - i) { Console.Write(" "); } else { Console.Write("#"); } } Console.WriteLine(); } Console.ReadKey(true); }

**Method 2: (Optimized)**

This one was suggested by **navule**, in comment section. In terms of performance this is far better than Method 1.

Using String constructor, we can print stair case in single line. Below is the algorithm that we are using to print stair case pattern.

**Algorithm:**

String constructor takes two argument, 1st argument is character that you wanna show, 2nd argument is number of time you want to replicate it.

Our requirement is to print required white spaces first and then #. For example,

- First iteration we need to print 5 white spaces and then # (total characters must be 6).
- Second iteration, we need to print 4 white spaces and ##.
- Third iteration, we need to print 3 white spaces and ###.
- Forth iteration, we need to print 2 white spaces and ####.
- Fifth iteration, we need to print 1 white spaces and #####.
- Sixth iteration, 0 white space and ######.

To achieve above algorithm, and printing white spaces and # at once we are using String Constructor. Using first constructor we are printing numbers of white spaces; and using second we are printing #’s.

public static void PrintStairCaseOptimized(int n) { for (int i = 1; i <= n; i++) { Console.WriteLine(new String(' ', n - i) + new String('#', i)); } }

static void StairCase(int n) {

for(int i=1; i<=n; i++){

Console.WriteLine(new String(' ',n-i) + new String('#',i));

}

}

LikeLike

Good one, better in performance too. Thanks for suggesting this, I’m gonna include this in this post.

LikeLike

class StairCaseProblem

{

public static void staircase(int number)

{

int i, j, k;

for (i = 1; i <= number; i++)

{

for (j = 1; j <= number – i; j++)

{

Console.Write("");

}

for (k = 1; k <= i; k++)

{

Console.Write("*");

}

Console.WriteLine("");

}

for (i = 1; i <= number; i++)

{

for (j = 1; j <= number – i; j++)

{

Console.Write(" ");

}

for (k = 1; k <= i; k++)

{

Console.Write("*");

}

Console.WriteLine("");

}

}

}

}

static void Main(string[] args)

{

//staircase problem

int t = Convert.ToInt16(Console.ReadLine());

StairCaseProblem.staircase(t);

Console.readKey();

}

LikeLike

Nice try Priyanka, but I don’t think so we need 2 nested loops. We can achieve the same using single loop and that will definitely improve performance of your code.

LikeLike

It is working….but may i know the explanation of the code please??

LikeLike

Updated post with comments. Hope now you can understand it now.

LikeLike