Triangle, NorthEast Learn programming C#



Lesson:

Basic Data Types


Exercise:

Triangle, NorthEast


Objetive:

Write a C# program which asks for a width, and displays a triangle like this one:

Enter the desired width: 5

*****
_****
__***
___**
____*


Code:

using System;
public class exercise62
{
    public static void Main()
    {
        int width, height;
        int row, column;
        int max;

        Console.Write("Enter the desired width: ");
        height = Convert.ToInt32(Console.ReadLine());
        width = 0;
        max = height;

        for (row = 0; row < height; row++)
        {
            for (column = 0; column < width; column++)
                Console.Write(" ");

            for (int asterisks = 0; asterisks < max; asterisks++)
                Console.Write("*");

            Console.WriteLine();
            width++;
            max--;
        }
    }
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.