Do you want to print an alphabet triangle in C#? In this C# tutorial, I will explain how to write a C# program to print alphabet triangle with complete code.
To create an alphabet triangle in C#, you can use nested loops: an outer loop to handle the number of lines, and an inner loop to print characters in each line. In the inner loop, increment a character variable starting from ‘A’ to create the alphabetical sequence, and use Console.WriteLine()
to move each level of the triangle to a new line.
C# Program to Print Alphabet Triangle
Now, let us write a C# program that prints an alphabet triangle.
An alphabet triangle is a pattern where alphabets are arranged in a triangular shape. For example, a simple alphabet triangle might look like this:
A
AB
ABC
ABCD
ABCDE
Here’s a step-by-step guide to writing the program to print alphabet triangle in C#.
Step 1: Define the Main Class
First, we need to define a class with the Main method. In C#, the Main method is the entry point of the program.
using System;
class AlphabetTriangle
{
static void Main(string[] args)
{
// Our code will go here
}
}
Step 2: Initialize Variables
We’ll need a variable to determine the size of the triangle. Additionally, we’ll use a character variable to print alphabets.
int size = 5; // You can change this to increase or decrease the size of the triangle
char letter;
Step 3: Create the Triangle Logic
We use two nested loops: the outer loop to handle the number of lines and the inner loop for printing characters in each line.
for (int i = 1; i <= size; i++)
{
letter = 'A';
for (int j = 1; j <= i; j++)
{
Console.Write(letter);
letter++;
}
Console.WriteLine();
}
In the inner loop, we increment the letter
variable to move to the next alphabet. The Console.WriteLine()
at end of the outer loop ensures that each level of the triangle starts on a new line.
Here is the complete program to print alphabet triangle in C#.
using System;
class AlphabetTriangle
{
static void Main(string[] args)
{
int size = 5; // Size of the triangle
char letter;
for (int i = 1; i <= size; i++)
{
letter = 'A';
for (int j = 1; j <= i; j++)
{
Console.Write(letter);
letter++;
}
Console.WriteLine();
}
}
}
You can check the output in the screenshot below after I ran the code using a console application in Visual Studio.
In this program:
size
defines the height of the triangle. You can adjust this to increase or decrease the triangle’s size.- The outer loop (
for (int i = 1; i <= size; i++)
) controls the number of rows. - The inner loop (
for (int j = 1; j <= i; j++)
) prints the alphabets in each row, incrementing theletter
variable in each iteration. Console.WriteLine()
creates a new line after each row is printed, forming the triangle shape.
output
A
AB
ABC
ABCD
ABCDE
Conclusion
In this C# tutorial, I have written a C# program that prints an alphabet triangle. I have executed the program using a console application in C# with Visual Studio editor.
You may also like:
- How to Format Number Thousand Separator with Space in C#?
- Write A C# Program To Reverse The Words Of A Sentence
- Write A C# Program To Check Palindrome Number
- Write A C# Program To Check Prime Number
- Write A Program To Demonstrate Type Casting In C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…