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;
}

12 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
  8. static void Main(string[] args)
    {
    int n = Convert.ToInt16(Console.ReadLine());
    for (int i = n; i > 0; i--)
    {
    for(int j=1;j<=n;j++)
    {
    if(i-j>=1)
    {
    Console.Write(" ");
    }
    else
    {
    Console.Write("#");
    }
    }
    Console.WriteLine();
    }
    Console.ReadKey();
    }

    ReplyDelete
  9. int main(){
    int n;
    cin >> n;

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

    ReplyDelete
  10. solution.cc: In function ‘int main()’:
    solution.cc:47:5: error: redefinition of ‘int main()’
    int main() {
    ^~~~
    solution.cc:30:5: note: ‘int main()’ previously defined here
    int main () {
    ^~~~
    solution.cc:52:17: error: ‘StairCase’ was not declared in this scope
    StairCase(_n);
    ^

    ReplyDelete
  11. int main(){
    int n;
    cin >> n;

    int i = 1;
    while (i <= n) {
    for (int j = i+1; j <= n; j++) cout << " ";
    for (int k = n-i; k < n; k++) cout << "#";
    cout << endl;
    i++;
    }

    return 0;
    }

    ReplyDelete
  12. === one loop ===


    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;


    int main() {
    int N;
    cin>>N;
    string s("");
    for (int i=0;i<N;i++){
    s+="#";
    cout<<setw(N)<<s<<endl;
    }
    return 0;
    }

    ReplyDelete