Exercise
Triangle Right Side
Objective
Write a C# program that asks the user for a string and displays a right-aligned triangle:
____n
___an
__uan
Juan
Write Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace to use its functionality
class Program // Define the main class
{
static void Main() // The entry point of the program
{
Console.Write("Enter your name: "); // Prompt the user to input their name
string name = Console.ReadLine(); // Read the input from the user
// Call the method to display the right-aligned triangle with the name
DisplayRightAlignedTriangle(name);
}
// Method to display the right-aligned triangle
static void DisplayRightAlignedTriangle(string text)
{
int length = text.Length; // Get the length of the input string
// Loop to print each line of the triangle
for (int i = 1; i <= length; i++) // i represents the number of characters to print in each row
{
// Print the spaces for the right alignment
for (int j = 0; j < length - i; j++)
{
Console.Write(" "); // Print spaces
}
// Print the substring of the name up to the current character (i)
Console.WriteLine(text.Substring(0, i)); // Display the substring of the text for the current row
}
}
}
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#.
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...
This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" 80 letters (e.g., 'X') in random positions withi...
This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" a circumference with a radius of 8 inside it. To...
Exercise in C# involves creating a program that can store up to 1000 records of computer programs. For each program, the following data should be stored: name,...