Sunday, 16 August 2015

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
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
     #
    ##
   ###
  ####
 #####
######

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

Solution:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int n,sp,w = 1,k;
    cin >> n;
    k = n-1;
    for(int i = 0; i < n; i++){
     sp = k;
     for(int j = 0; j < sp; j++)cout << " ";
     for(int m = 0; m < w; m++)cout << "#";
     cout << endl;
     k--;
     w++;
    }
    return 0;
}

7 comments:

  1. int main(){
    int n, b=1;
    cin >> n;
    int a=n-1;

    for(int i=0; i<n; i++)
    {
    for(int j=0; j < a; j++){
    cout<<" ";
    }

    for(int k=0; k < b; k++)
    {
    cout<<"#";
    }
    cout<<endl;
    b++;
    a--;

    }
    return 0;
    }

    ReplyDelete
  2. #include
    using namespace std;

    int main() {
    int n = 10;

    for (int i = 1; i < n; i++)
    {
    int sp = n - i;
    while (sp--)
    cout <<" ";
    for (int j = 0; j < i; j++)
    cout << "#";
    cout << '\n';
    }
    return 0;
    }

    ReplyDelete
  3. int main(){
    int n;
    cin >> n;
    int hashCount = 0;
    for(int i = 1; i <= n; ++i) {
    int count = n-i;
    for(count; count >0; --count) {
    cout << " ";
    }
    for(count; count < i; ++count) {
    cout << "#";
    }
    cout << endl;
    }
    }

    ReplyDelete
  4. int main(){
    int n;
    cin >> n;
    int a=n-1;

    for(int i=1; i<=n; i++)
    {
    for(int j=0; j < a; j++){
    cout<<" ";
    }

    for(int k=0; k < i; k++)
    {
    cout<<"#";
    }
    cout<<endl;
    a--;

    }
    return 0;
    }

    ReplyDelete
  5. #include
    #include
    #include
    #include
    #include
    using namespace std;


    int main(){
    int n;
    cin >> n;
    for(int i = 0; i<n;i++)
    {
    int a =i;
    for(int j =i+1;j<n;j++)
    {
    cout<<" ";
    }
    for(int k=0;k<=a;k++)
    {
    cout<<"#";
    }
    cout<<endl;
    }
    return 0;
    }

    ReplyDelete
  6. #include
    #include
    #include
    #include
    #include
    using namespace std;


    int main(){
    int n;
    cin >> n;

    for (int i=1; i<=n; i++)
    {
    for (int j=n; j>=1; j--)
    {
    if (j<=i)
    cout << "#";
    else
    cout << " ";
    }
    cout << endl;
    }
    return 0;
    }

    ReplyDelete
  7. can u plss explain me the logic of this problem

    ReplyDelete