C# program to print triangle with different symbols and letters (smiley, copyright, star, numbers, alphabets) on console


Here in this post we are going to learn, how to print triangle on console using C#.net.

Outputs we are going to learn:

printing triangle with white smiley symbol on consoleprinting triangle with black smiley symbol on consoleprinting triangle with copyright symbol on consoleprinting triangle with star symbol on consoleprinting triangle with numbers on consoleprinting triangle with numbers on consoleimageimage

So we will print the above outputs on the console using C#.Net

How are we going to implement:

1. we will ask the user to enter Number of rows to print.

2. then we will print the pattern

3. to print the special symbols like smiley and copyright I have used Unicode characters and for that we need to mark output encoding as below

Marking output encoding
Console.OutputEncoding = System.Text.Encoding.UTF8;

4. I have given only 3 examples with symbols and if you want to try with more you can get the Unicode characters from the below link

https://www.compart.com/en/unicode/category/So?page=4

Triangle with Star symbol
using System;

namespace AlgorithamsAndLogics
{
    class TrianglePattern
    {
        static void Main(String[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Howmany number of rows do you want to print?");
            int numberOfRows = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= numberOfRows; i++)
            {
                int starCount = 0;
                for (int j = 1; j <= numberOfRows – i; j++)
                {
                    Console.Write(" ");
                }
                while (2 * i – 1 != starCount)
                {
                    Console.Write("*");
                    starCount++;
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }
}

Output:

image

Triangle with numbers
using System;

namespace AlgorithamsAndLogics
{
    class TrianglePattern
    {
        static void Main(String[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Howmany number of rows do you want to print?");
            int numberOfRows = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= numberOfRows; i++)
            {
                int starCount = 0;
                for (int j = 1; j <= numberOfRows – i; j++)
                {
                    Console.Write(" ");
                }
                while (2 * i – 1 != starCount)
                {
                    Console.Write(i);
                    starCount++;
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }
}

Output:

image

Triangel with numbers
using System;

namespace AlgorithamsAndLogics
{
    class TrianglePattern
    {
        static void Main(String[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Howmany number of rows do you want to print?");
            int numberOfRows = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= numberOfRows; i++)
            {
                int starCount = 0;
                for (int j = 1; j <= numberOfRows – i; j++)
                {
                    Console.Write(" ");
                }
                while (2 * i – 1 != starCount)
                {
                    Console.Write(starCount);
                    starCount++;
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }
}

Output:

image

Triangle with Copyright symbol
using System;

namespace AlgorithamsAndLogics
{
    class TrianglePattern
    {
        static void Main(String[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Howmany number of rows do you want to print?");
            int numberOfRows = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= numberOfRows; i++)
            {
                int starCount = 0;
                for (int j = 1; j <= numberOfRows – i; j++)
                {
                    Console.Write(" ");
                }
                while (2 * i – 1 != starCount)
                {
                    Console.Write('\u00A9');
                    starCount++;
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }
}

Output:

image

Triangle with black smiley
using System;

namespace AlgorithamsAndLogics
{
    class TrianglePattern
    {
        static void Main(String[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Howmany number of rows do you want to print?");
            int numberOfRows = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= numberOfRows; i++)
            {
                int starCount = 0;
                for (int j = 1; j <= numberOfRows – i; j++)
                {
                    Console.Write(" ");
                }
                while (2 * i – 1 != starCount)
                {
                    Console.Write('\u263A');
                    starCount++;
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }
}

Output:

image

Triangle with white smiley
using System;

namespace AlgorithamsAndLogics
{
    class TrianglePattern
    {
        static void Main(String[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Howmany number of rows do you want to print?");
            int numberOfRows = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= numberOfRows; i++)
            {
                int starCount = 0;
                for (int j = 1; j <= numberOfRows – i; j++)
                {
                    Console.Write(" ");
                }
                while (2 * i – 1 != starCount)
                {
                    Console.Write('\u263B');
                    starCount++;
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }
}

Output:

image

Triangle with alphabets
using System;

namespace AlgorithamsAndLogics
{
    class TrianglePattern
    {
        static void Main(String[] args)
        {
            int ascii = 65;
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Howmany number of rows do you want to print?");
            int numberOfRows = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= numberOfRows; i++)
            {
                int starCount = 0;
                for (int j = 1; j <= numberOfRows – i; j++)
                {
                    Console.Write(" ");
                }
                while (2 * i – 1 != starCount)
                {
                    Console.Write(Convert.ToChar(ascii + starCount));
                    starCount++;
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }
}

Output:

image

Triangle with alphabets
using System;

namespace AlgorithamsAndLogics
{
    class TrianglePattern
    {
        static void Main(String[] args)
        {
            int ascii = 65;
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Howmany number of rows do you want to print?");
            int numberOfRows = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= numberOfRows; i++)
            {
                int starCount = 0;
                for (int j = 1; j <= numberOfRows – i; j++)
                {
                    Console.Write(" ");
                }
                while (2 * i – 1 != starCount)
                {
                    Console.Write(Convert.ToChar(ascii + i – 1));
                    starCount++;
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }
}

Output:

image

Happy coding..!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s