Visual C# 6.16 (Displaying a Bar Chart)

This Solution is for the “Displaying a Bar Chart” of chapter 6 in the textbook; Visual c# How to program 6th edition by Deitel.

//Date: 5/9/17
/*Description:
Reads 3 numbers between 1 & 30. For each number that’s read
the same number of adjacent asterisks are displayed.
*/
using System;

class Program
{
static void Main()
{
// for statement that manages the tasks that prompt the user 3 times for numbers between 1 & 30
for (int read = 0; read != 3; ++read)
{
Console.Write(“Please enter the Length of your desired asterisk bar between 1 & 30: “);
int size = int.Parse(Console.ReadLine());

//validates input
if (size > 30)
{
//if neccesary sets input to an appropriate size
size = 30;
// then prompts user of the change
Console.WriteLine(“Your input was too large and has been truncated to 30.”);
}

else if (size < 1)
{
size = 1;
Console.WriteLine(“Your input was too small and has been increased to 1.”);
}
//nested for statement that manages the printing of asterisks of the appropriate amount
for (int counter = 0; counter < size; counter++)
{
Console.Write(“*”);
}
Console.WriteLine();
}
Console.WriteLine();
}
}

Program Output

Test #1

Please enter the Length of your desired asterisk bar between 1 & 30: 7
*******
Please enter the Length of your desired asterisk bar between 1 & 30: 21
*********************
Please enter the Length of your desired asterisk bar between 1 & 30: 4
****

Press any key to continue . . .

—————————————————————————-
Test #2 validated input:

Please enter the Length of your desired asterisk bar between 1 & 30: 50
Your input was too large and has been truncated to 30.
******************************
Please enter the Length of your desired asterisk bar between 1 & 30: -10
Your input was too small and has been increased to 1.
*
Please enter the Length of your desired asterisk bar between 1 & 30: 17
*****************

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