Making pyramid using c#

后端 未结 13 2141
花落未央
花落未央 2021-01-03 11:04

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

     *
    * *
   * * *
  * * * *
 * * * * *
<
13条回答
  •  日久生厌
    2021-01-03 11:16

    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

提交回复
热议问题