# How to Print Staircase Pattern in C++

In this article, we are going to see how to **print the staircase pattern in C++**. For doing this, we will use the concept of nested loops.

## Example of Staircase Pattern

Consider the height of the staircase to be 5.

Then the staircase looks as follows:

# ## ### #### #####

In a staircase pattern with height h, the number of # symbols in the last row is equal to h. Note that the last row is not proceeded by any spaces. Also, observe that the sum of the number of spaces and the number of # symbols in each row is equal to h.

## Implementation in C++

#include <iostream> using namespace std; void staircase (int n) { for (int i = 0 ; i < n ; i++) { for (int j = i; j < (n-1) ; j++) cout << " "; for (int k = 0 ; k < (i+1) ; k++) cout << "#"; cout << "\n"; } } int main() { int n; cout << "Enter the height of the required staircase pattern: "; cin >> n; cout << "The staircase pattern is as follow: " << endl; staircase(n); return 0; }

Firstly, let us get the height of the staircase pattern from the user.

We will use one loop to iterate over the height(number of rows) of the staircase. We will use two inner for loops one for printing spaces and the other for printing # symbols.

In the first row, we need to print height-1 spaces followed by one # symbol. So, the number of spaces we need to print in each row is equal to the (row index number)+(height-1). And the number of # symbols which follow these spaces is equal to (row index number)+1.

Output:

Enter the height of the required staircase pattern: 8 The staircase pattern is as follow: # ## ### #### ##### ###### ####### ########

This is how we print the staircase pattern in C++.

Also read, Get IP address from hostname in C++ for Windows

Height of a binary tree in C++

## Leave a Reply