Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Staircase Hacker Rank Problem Solution Using C++. Problem Statement Your teacher

ID: 3719065 • Letter: S

Question

Staircase Hacker Rank Problem Solution Using C++.

Problem Statement

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

Sample Output


TAG: C++,Implementation , Simple Program , Hacker Rank.

I am wondering how you would write the pseudocode of the following source code. Please write the pseudocode of the source code and also explain the reasoning behind both the pseudocode and the source code.
Solution:

Explanation / Answer

Hey, please find the source code explanation with pseudo code below.

1. Lets assume that the input=6, which means we have to print 6 different lines.

2. Now lets talk about the first line we need to print only 1 # and print " " (space) 5 times, i.e input - line number (6-1=5).

3. Now for the first line we will print spaces first from start till 5 (input - line number) and after that will start to print # 1 time (which is equal to line number, in first line it it 1);

4.Then after printing 1st line, we will move to the second line.

5. In second line we have to print 4 spaces (input-line number) and after that 2 # (equal to line number).

6. Similary we will be printing for all the subsequent lines till the input value, which is 6 in this case. We observed here that number of spaces reduces by 1 and number of # increases by 1, as we go down.

# Explanation of code :

int main() {
//Declare Variables
int n,sp,w = 1,k;
//Take input from console
cin >> n;
//Now initialize k with n-1, as we will start the loop from 0.
k = n-1;
//Loop from 1st line to the last line starting from 0 to n-1, Printing 1 line at a time.
for(int i = 0; i < n; i++){
  
sp = k;
//Printing spaces for the given line from 0 till sp-1
for(int j = 0; j < sp; j++)cout << " ";
//Printing # for the given line from sp till w
for(int m = 0; m < w; m++)cout << "#";
//now moving to the next line
cout << endl;
//decrementing k so that printing " " decrements by 1 as we go down after each iteration.
k--;
//incrementing w so that printing "#" increments by 1 as we go down after each iteration.
w++;
}
return 0;
}

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote