Visual C# 5.30 (Square of Asterisks)

This Solution is for the “Square of Asterisks” of chapter 5 in the textbook; Visual c# How to program 6th edition by Deitel.

//Author: Alexander Anaya Ashe
//Class: MIS118 Spring 2017
//Date: 5/5/17
/*Description:
program prompts user to enter the size of 1 side of a square; any number between 1 & 20.
program then prints a square respective to the size provided by the user. If the input number
is less than 1 or more than 20, the program prints a square of 1 or 20 respectively
*/
using System;

class Program
{
public static void Main()
{

int loopcounter = 1;//sets initial variable
int inrloopcounter;

// prompts user for input and obtains value for size from the user
Console.Write(“Please enter the size of your desired square: “);
int size = int.Parse(Console.ReadLine());

//validates input
if (size > 20)
{ size = 20; }
//if neccesary sets input to an appropriate size
if (size < 1)
{ size = 1; }

//begins while statement to create the box
while (loopcounter <= size)//repeats for as many times as the size specifies
{
inrloopcounter = 1; // sets the variable to 1 every time statement is run

//repeats the nested while statement for as many times as the size specifies
while (inrloopcounter <= size)
{
//if else statments that place stars repective to the if statements met
if (loopcounter == 1)
Console.Write(“* “);
else if (loopcounter == size)
Console.Write(“* “);
else if (inrloopcounter == 1)
Console.Write(“* “);
else if (inrloopcounter == size)
Console.Write(“* “);

else //if none of the if or else statements apply prints blank
Console.Write(” “);
//adds 1 to variable
++inrloopcounter;
}
Console.WriteLine();
++loopcounter; //adds 1 to variable

}
}
}

Output Data:

Please enter the size of your desired square: 50
* * * * * * * * * * * * * * * * * * * *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
* * * * * * * * * * * * * * * * * * * *
Press any key to continue . . .

—————————————————–
Please enter the size of your desired square: 7
* * * * * * *
*                *
*                *
*                *
*                *
*                *
* * * * * * *
Press any key to continue . . .

————————————————

Please enter the size of your desired square: -4
*
Press any key to continue . . .

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s