Sort by

recency

|

5180 Discussions

|

  • + 0 comments

    My solution in C# builds the staircase row by row. For each row i (from 1 to n):

    I print (n - i) spaces to align it to the right

    Then I print i hash symbols (#)

    This way the output grows step by step until the full staircase is printed.

        public static void staircase(int n)
    {
        for(int i =1; i <= n; i++){
            int spaces = n - i;
            int hashtags = i;
            string staircase = new string(' ', spaces) + new string('#', hashtags);
            Console.WriteLine(staircase);
        }
    }
    
  • + 0 comments

    Here is my solution in C (I know could be better) time complexity: O(n^2)

    void staircase(int n) {

    int m = 0;
    
    for(int i = 1 ; i <= n ; i++)
    {
        m++;
        for(int j = 0 ; j < n - i ; j++)
        {
            printf(" ");
        }
    
        for(int k = 0 ; k < m ; k++)
        {
            printf("#");
        }
    
        printf("\n");
    }
    

    }

  • + 0 comments

    var sb=new StringBuilder();

        for(var i=1;i<=n;i++)
        {
            sb.Append(new String(' ',n-i));        
            sb.Append(new String('#',i)+"\n");
        }
    
        Console.Write(sb.ToString());
    
  • + 0 comments

    Ussing operator overloading for fun, we all know it is not necessary.

    string operator*(const std::string& str, int count){
        std::string result = "";
        for (int i = 0; i < count; ++i) {
            result += str;
        }
        return result;
    }
    
    string operator*(int count, const string& str) {
        return str * count;
    }
    
    void staircase(int n) {
        int steps = n;
        while (n-->0) {
            cout<<" "s*n + "#"s*(steps-n)<<endl;
        }
    }
    
  • + 0 comments

    for (int i = 1; i <= n; i++) { string bosluklar = new string(' ', n - i); string kareler = new string('#', i); Console.WriteLine(bosluklar + kareler); }