C# Basic: Nested Loop (Display Star Right Triangle)

Today I will show you how to use a nested loop. To demonstrate a nested loop we will create a star triangle pattern.

Our output will look like this.

Advertisements

First we will ask the user to enter the height of the triangle. We can get a user input by using the Console.ReadLine(). Console.ReadLine() returns a string value, but we need an int value for our height. So we need to convert the value of Console.ReadLine() to an int value and to do that we can use int.Parse. Our code will look like this.

using System;

namespace StartPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the height of triangle:");
            int height = int.Parse(Console.ReadLine());
        }
    }
} 

Now that we have our height. Let’s display an * using for loop. Add this code in your Main method.

for(int i = 1; i <= height; i++)
            {
                Console.Write("*");
            }

Then try to run your application. You should get this output.

As you can see in the output. You can now display the same number of * as your height value. Since this the height of the triangle, this should be displayed vertically. To do that add this code inside your for loop.

Console.WriteLine();

Now it is displayed vertically, but to make it a triangle we still need to display * horizontally.

Before we can make our triangle we need to identity how many * we need to display per line. In the first line we need one *, and two * in second line. The same thing apply in other lines.

Now we know that the number of * per line is the same as the line number. Let’s move our Console.Write(“*”) inside a for loop and use the value of i as for limit of our for loop.

for(int i = 1; i <= height; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

Run your application now. It should be able to display this.

This is our entire source code for this application.

using System;

namespace StartPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the height of triangle:");
            int height = int.Parse(Console.ReadLine());
            for(int i = 1; i <= height; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
    }
} 

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