# Staircase code in C#

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
1N100

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:

1. Get the number of total rows or maximum number of #s that we need to print from s standard input.
2. Loop over total number of rows because we need to print white spaces and # in each line.
3. 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.
4. 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.
5. 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.
6. 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.
7. 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.
8. 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

// 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();
}
}

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,

1. First iteration we need to print 5 white spaces and then # (total characters must be 6).
2. Second iteration, we need to print 4 white spaces and ##.
3. Third iteration, we need to print 3 white spaces and ###.
4. Forth iteration, we need to print 2 white spaces and ####.
5. Fifth iteration, we need to print 1 white spaces and #####.
6. 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));
}
}

## 6 thoughts on “Staircase code in C#”

1. navule says:

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

Like

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

Like

2. Priyanka H says:

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
StairCaseProblem.staircase(t);
}

Like

1. 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.

Like

3. pavithra says:

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

Like

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

Like