Print star ('*') diamond in C with nested loops?

前端 未结 10 1784
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 14:59

I want to be able to print a diamond like this when the user enters 5 for the diamond. But also will work for any value that is odd and greater than 0.

10条回答
  •  情深已故
    2021-01-07 15:30

     static void Main(string[] args)
            {    //check this out more optimized code hope it will be help full.    
        for (int row=-2;row<=2;row++)
            {
            for (int col=-2;col<=2;col++)
            {
            if (Math.Abs(row)+Math.Abs(col)<=2)
            {
            Console.Write("*");
            }
            else
            {
            Console.Write(" ");
            }
            }
            Console.WriteLine();
            }
                Console.ReadKey();
            }
    

提交回复
热议问题