Upper triangular matrix

Write a Program to display the upper triangular matrix

For example

Input 
[1,2,3]
[4,5,6]
[7,8,9]

Output:
[1,2,3]
[0,5,6]
[0,0,9]

Solution:


using System;

namespace TriangularMatrix
{
    class Program
    {
        static void Main()
        {
            int[,] a = {
                {1, 4, 7},
                {2, 5, 8},
                {3, 6, 9}
            };

            var rows = a.GetLength(0);
            var cols = a.GetLength(1);


            Console.WriteLine("Upper Triangular Matrix: ");
            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    Console.Write(i > j ? "0\t" : a[i, j] + "\t");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *