C# Program to Display Upper Triangular Matrix

This is a C# Program to display upper triangular matrix.

Problem Description

This C# Program Displays Upper Triangular Matrix.

Problem Solution

A square matrix is upper triangular matrix if all its entries below the main diagonal are zero.

Program/Source Code

Here is source code of the C# Program to Display Upper Triangular Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Display Upper Triangular Matrix 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
    class Program
    {
        int x;
        public static void Main(string[] args)
        {
            int m, n, i, j;
 
            Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : ");
            m = Convert.ToInt16(Console.ReadLine());
            n = Convert.ToInt16(Console.ReadLine());
            int[,] A = new int[10, 10];
            Console.Write("\nEnter The First Matrix : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    A[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }          
            Console.Clear();
            Console.WriteLine("\nMatrix A : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(A[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
 
            Console.WriteLine("\n Setting Zero to illustrate Upper "+ 
                              "Triangular Matrix\n");
            for (i = 0; i < m; i++)
            {
                Console.Write("\n");
                for (j = 0; j < 3; j++)
                {
                    if (i <= j)
                        Console.Write(A[i, j] + "\t");
                    else
                        Console.Write("0\t");
                }
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program, we are reading the order of the matrix row and column using ‘m’ and ‘n’ variables respectively. Using for loop we are entering the coefficient values of the matrix to the variable A[i,j]. To display upper triangular matrix two for loops are used.

advertisement

In first for loop the value of ‘i’ variable is initialized to zero and execute the loop till the value of ‘i’ variable becomes less than the value of ‘row’ variable. In another for loop the value of ‘j’ variable is initialized to zero and execute till the value of ‘j’ variable becomes less than the value of ‘column’ variable. Print the upper triangular matrix.

Runtime Test Cases
 
Enter Number Of Rows And Columns Of Matrices A and B : 3  3
Enter the First Matrix : 
1 2 3
2 3 4
3 4 5
Matrix A :
1 2 3
2 3 4
3 4 5
Setting Zero to illustrate Upper Triangular Matrix :
1 2 3
0 3 4
0 0 5

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Free 30-Day C Certification Bootcamp is Live. Join Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.