182k views
0 votes
In C#

Print isosceles triangles. For each triangle, allow the user to input two values: a character to be used for printing the triangle and the size of the peak for the triangle. Test the input for valid characters. The size of the triangle should not be larger than 10. If an invalid non-numeric character is entered for size or if the value entered for size is larger than 10, use 3 as the default value. If an invalid entry is entered for the character, use an asterisk ( * ) as the default character. Allow multiple triangles to be printed. For example, if the user inputs # for the character and 6 for the peak, you should produce the following display:

#
##
###
####
#####
######
#####
####
###
##
#

Added to the specifications from the textbook, instead of allowing the user to input the character for printing, you should use your first name's initial as the character. Define this value as a constant. Addtionionally include as part of the final display a heading that includes your name along with the label "Isosceles Triangle". So, for example, my triangle would use all K's (instead of the # character) and be labeled "K's Isosceles Triangle"

User Ivan Sudos
by
7.9k points

1 Answer

7 votes

Final answer:

To create an isosceles triangle in C# using the initial of the tutor's name as the character and ensuring the peak size does not exceed 10, loops and input validation are used. Defaults are set for invalid character or size entries, and a heading is included.

Step-by-step explanation:

To print an isosceles triangle in C# with a peak size not larger than 10, you would need to use loops to create the desired pattern. As an additional requirement, we are to use the initial of the tutor's first name as a constant character to print the triangle and include a heading that labels it as 'Name's Isosceles Triangle'. If the user inputs an invalid non-numeric character or a value greater than 10 for the peak size, or an invalid character, defaults of '3' and '*' respectively will be used. Here is a simplified version of what the code could look like:

const char TRIANGLE_CHAR = 'T'; // Replace 'T' with the initial of your name
const int MAX_SIZE = 10;

string heading = "T's Isosceles Triangle"; // Replace 'T' with the initial of your name
Console.WriteLine(heading);

Console.Write("Enter the size of the triangle's peak (up to 10): ");
int size;
if (!int.TryParse(Console.ReadLine(), out size) || size > MAX_SIZE)
{
size = 3;
}

for (int i = 1; i <= size; i++)
{
string line = new string(' ', size - i) + new string(TRIANGLE_CHAR, i * 2 - 1);
Console.WriteLine(line);
}

Remember to validate user input for the size, use loops to create the triangle shape, and initialize the character as a constant.

User Interface Unknown
by
8.2k points