Exercise
Centered Triangle
Objective
Write a C# program that Display a centered triangle from a string entered by the user:
__a__
_uan_
Juan
Write Your C# Exercise
C# Exercise Example
using System; // Import the System namespace for basic functionality
class Program // Define the main class
{
static void Main() // The entry point of the program
{
// Ask the user to input their name
Console.Write("Enter your name: ");
string name = Console.ReadLine(); // Read the user's input as a string
// Loop through the string and print each progressively longer substring, centered
for (int i = 1; i <= name.Length; i++)
{
// Create the substring from the first character to the current position
string substring = name.Substring(0, i);
// Calculate the number of spaces to center the substring
int spaces = (name.Length - i) / 2;
// Print the substring with the calculated number of spaces for centering
Console.WriteLine(new string(' ', spaces) + substring + new string(' ', spaces));
}
}
}
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this exercise, you are asked to create a database to store information about cities. In the first approach, you will store only the name...
This Exercise in C# involves creating a program that imitates the basic Unix SysV "banner" utility, allowing you to display large texts in a similar manner. The purpo...
This Exercise in C# involves creating a program that asks the user for a text string and displays a right-aligned triangle using that string. The triang...
This Exercise in C# involves creating a program that asks the user for a text string and performs three specific transformations on it. The program must...
This Exercise in C# involves creating a struct to store two pieces of data for a person: their name and their date of birth. The date of birth...
This Exercise in C# involves creating a program that asks the user for 10 integer numbers (from -1000 to 1000), sorts them, and then displays them in ascending...