Triangle Right Side - C# Programming Exercise

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 triangle should have the shape of a text pattern where, as it progresses, the string is truncated in each line. The goal of this Exercise is to practice handling text strings and using control structures such as loops in C# to achieve a specific format in the output.

In this Exercise, the program will take the user's input and generate a text pattern that is right-aligned like a triangle. For example, if the user enters "Juan", the program should display:

____n
___an
__uan
Juan

This type of project is a great way to improve your C# skills and get familiar with using loops to modify the output format as needed.

 Category

Arrays, Structures and Strings

 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

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
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

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#.

  •  Strings manipulation

    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...

  •  Nested structs

    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...

  •  Sort data

    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...

  •  Two dimensional array as buffer for screen

    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...

  •  Two dimensional array 2: circunference on screen

    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...

  •  Computer programs

    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,...