c# home works can’t solve [closed]

you can do the following:

explanation for the code:

Square is formed of 2 equal sides, so in order to draw the square using *, you will think of it like a matrix

you need a loop to draw the rows and another loop to draw the columns, and both loop has upper limit which is the number entered by the user.

the rule of drawing is like this i will put star if i am in the first row or the last one or in the first column or last, so by addressing this i used the if statement ( i==0 || i== number-1 || j==0 || j== number -1) where i is the row and j is the column, and if this condition is not satisfied, print space

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Please enter a number:");
        var number=Convert.ToInt32(Console.ReadLine());
        for(int i=0; i < number; i++)
        {           
            for(int j=0; j < number; j++)
            {           
                if(i==0 || i == number-1 || j==0 || j == number-1)
                    Console.Write("*"); 
                else
                    Console.Write(" ");
            }
            Console.Write("\n");
        }
    }
}

here a working DEMO

hope this will help you

Leave a Comment