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

前端 未结 10 1980
醉酒成梦
醉酒成梦 2021-01-07 14:41

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条回答
  •  Happy的楠姐
    2021-01-07 15:29

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

提交回复
热议问题