'Making pyramid using c#

My question is how to make a pyramid using * and 'space' in C#? The output will be like this.

     *
    * *
   * * *
  * * * *
 * * * * *

We only need to use "for loop" for this program. I only know how to make this one.

*
**
***
****
*****

I made a program like this:

static void Main(string[]args)
{
int i=o;
int j=o;

for(i=5;1>=1;i--)
  for(j=1;j<=5;j++)
   {
    Console.Write("*");
   }
  Console.WriteLine(" ");
}

I'm confused when it comes to pyramid because it includes spaces. Thanks for your help!



Solution 1:[1]

think about how you'd print the pyramid manually.

suppose 5 levels deep.

1st line: 4 spaces, 1 star,
2nd line: 3 spaces, star, space, star
3rd line: 2 spaces, star space star space star

etc.

doesn't matter whether you print spaces after the last star or not - won't make a difference to how it looks.

what do we see?

if we have a total of X levels

line 1: (x-1) spaces, (star space)
line 2: (x-2) spaces, (star space) twice
line 3: (x-3) spaces, (star space) three times
line 4: (x-4) spaces, (star space) four times

that's the pattern. I'll leave the coding to you.

Solution 2:[2]

using System;

    class Program
    {
        static void Main(string[] args)
        {
            int num, i, j, k;
            Console.Write("enter the level:");
            num=Convert.ToInt32(Console.ReadLine());
            for (i = 1; i <= num; i++)
            {
                for (j = 1; j < num-i+1; j++)
                {
                    Console.Write(" ");
                }
                for (k = 1; k <= i; k++)
                {
                    Console.Write(i);
                    Console.Write(" ");
                }
                Console.WriteLine();

            }
        }
    }

Solution 3:[3]

Your problem is spaces, therefore I suggest you think about the spaces. Tell me this: how many spaces are on each row to the left of the first star? You'll likely be able to solve your own problem if you think about this.

Solution 4:[4]

Try to think of it as a grid or a matrix and see where you want the '*' in each row and how it relates to your loop index.

Solution 5:[5]

sorry I missed this was homework... will give a strategy ... instead

it helps if you do it in notepad and think about what you are doing... you will start to understand the relationship between the line you are on and the spaces and what not...

Solution 6:[6]

Post my answer after 3 hours. I think now you have almost finished it under @iluxa's advice?

int height = 20;
for (int level = 1; level <= height; level++)
{
    string text = string.Join(" ", Enumerable.Repeat("*", level));
    Console.WriteLine(text.PadLeft(height - level + text.Length));
} 

I used some build-in methods e.g. Enumerable.Repeat and String.PadLeft, not the pure C-language way. The purpose is that I want to tell you since you have chosen C# as the programming language(not C/Java/etc), you should resolve problems in the C# way.

Solution 7:[7]

using System;               
using System.Collections.Generic;               
using System.Linq;

using System.Text;

namespace pyramid_star

{

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("enter a number:");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= n; i++)
            {
                for (int x = i; x <= n; x++)
                {
                    Console.Write(" ");
                }
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("*"+" ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

Solution 8:[8]

using System;
using System.Collections.Generic;
using System.Text;

namespace Star_Pyramid
{
    class Program
    {
        static void Main(string[] args)
        {
            Program o = new Program();
            o.show();

            Console.ReadKey();
        }
        void show()
        {
           for (int i = 1; i <= 12; i++)
            {
                for (int j = 1; j <= 9 - i / 2; j++)
                {
                    Console.Write("   ");
                }
                for (int k = 1; k <= i; k++)
                {
                    Console.Write(" * ");
                    Console.Write(" ");


                }
                Console.WriteLine();

            }
        }
    }
}

Solution 9:[9]

class Program

{

    static void Main(string[] args)

    {
        int num;
        Console.WriteLine("enter level");
        num = Int32.Parse(Console.ReadLine());
        int count = 1;

        for (int lines = num; lines >= 1; lines--)
        {

            for (int spaces = lines - 1; spaces >= 1; spaces--)
            {
                Console.Write(" ");

            }
            for (int star = 1; star <= count; star++)
            {
                Console.Write("*");
                Console.Write(" ");

            }
            count++;

            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

Solution 10:[10]

Try this and follow this same logic in c, c++, php, java

using System;

class pyramid {

      static void Main() {

           /** Pyramid stars Looking down 
               Comment this if u need only a upside pyramid **/

           int row, i, j;

           // Total number of rows
           // You can get this as users input 
           //row =  Int32.Parse(Console.ReadLine());
           row = 5;             

           // To print odd number count stars use a temp variable
           int temp;
           temp = row;

           // Number of rows to print 
           // The number of row here is 'row'
           // You can change this as users input 
           for ( j = 1 ; j <= row ; j++ ) { 

           // Printing odd numbers of stars to get 
           // Number of stars that you want to print with respect to the value of "i"?         
               for ( i = 1 ; i <= 2*temp - 1 ; i++ )
                   Console.Write("*");

           // New line after printing a row            
               Console.Write("\n");
               for ( i = 1 ; i <= j ; i++ )                
                   Console.Write(" ");

           // Reduce temp value to reduce stars count                  
               temp--;
           }

           /** Pyramid stars Looking up 
               Comment this if u need only a downside pyramid **/
           int rowx, k, l;

           // Total number of rows
           // You can get this as users input 
           // rowx =  Int32.Parse(Console.ReadLine());
           rowx = 5; 

           // To print odd number count stars use a temp variable
           int tempx;
           tempx = rowx;

           //Remove this if u use 
           Console.Write("\n");

           // Number of rows to print 
           // The number of row here is 'rowx'

           for ( l = 1 ; l <= rowx ; l++ ) {

           // Leaving spaces with respect to row
               for ( k = 1 ; k < tempx ; k++ )
                   Console.Write(" ");

           // Reduce tempx value to reduce space(" ") count  
               tempx--;

           // Printing stars 
               for ( k = 1 ; k <= 2*l - 1 ; k++ )
                   Console.Write("*"); 

           // New line after printing a row 
               Console.Write("\n");
           }           
      }
}

Solution 11:[11]

The following code might help:

public static void print(int no)
{
    for (int i = 1; i <= no; i++)
    {
        for (int j = i; j <= no; j++)
        {
            Console.Write(" ");
        }
        for (int k = 1; k < i * 2; k++)
        {
            if(k % 2 != 0)
            {
              Console.Write("*");
            }
            else
            {
              Console.Write(" ");
            } 
        }
        Console.WriteLine();
    } 
     Console.ReadLine();
}

Solution 12:[12]

Here I have created a number pyramid:

using System;

public class Program
{
   public static void Main()
   {
      Console.WriteLine("This is a number pyramid....");
      var rows = 5;

      for(int i = 1; i <= rows; i++)
      {
         for(int lsc = (-rows); lsc <= -2; lsc ++)
         {
            if(lsc < (-1)*i)
            {
               //write left sided blank spaces
               Console.Write(" ");
            }
            else
            {
               //write left sided numbers
               Console.Write(-1*(lsc));
            }
         }

         for(int rsc = 1; rsc <= rows; rsc++)
         {
            //write right sided blank spaces
            Console.Write(" ");
         }
         else
         {  
            //Write sided numbers
            Console.Write(rsc);
         }
       } 
      Console.WriteLine();
    }
  }
} 

I have described here https://utpalkumardas.wordpress.com/2018/04/20/draw-number-pyramid

Out put is:

The is a number pyramid....
        1
      212
    32123
  4321234
543212345

Solution 13:[13]

I know it's javascript but might help

let n = 6;
for (let i = 1; i <= n; i++) {
  let spaces = n-i;
  let str = '';
  for (let j=0; j < spaces; j++) {
    str+=' ';
  }
  
  for (let j=0; j < i; j++) {
    str+='* ';
  }
  console.log(str)
}

Solution 14:[14]

I have found two approaches:

       //1st approach
        int n = 6;
        for (int i = 0; i < n; i++)
        {
            // need to print spaces
            for (int j = 0; j < n - i - 1; j++)
            {
                Console.Write(" ");
            }
            // need to print * with one space
            for (int k = 0; k <= i; k++)
            {
                Console.Write("* ");
            }
            Console.WriteLine();
        }

        // 2nd Approach

        int rows = 6;
        int temp = 0;
        bool toggle = false;
        for (int j = 0; j < rows; j++)
        {
            int counter = 0;
            for (int i = 0; i < 2 * rows - 1; i++)
            {
                if (i < rows - temp - 1)
                {
                    Console.Write(" ");
                }
                else
                {
                    if (counter <= j + temp)
                    {
                        if (!toggle)
                        {
                            Console.Write("*");
                            toggle = true;
                        }
                        else
                        {
                            Console.Write(" ");
                            toggle = false;
                        }
                    }
                    counter++;
                }
            }
            Console.WriteLine();
            temp++;
            toggle = false;
        }