Question: Program to print the lower triangular matrix.
To print the Lower Triangle of the matrix first of all take the order of matrix it must be square, so, number of row and column oh the matrix is same, after that enter the element of the matrix after that print the matrix on the console.
Code Snippet:
To print the Lower Triangle of the matrix first of all take the order of matrix it must be square, so, number of row and column oh the matrix is same, after that enter the element of the matrix after that print the matrix on the console.
Code Snippet:
- using System;
- public class LowTrn
- {
- public static void Main()
- {
- int i, j, row, col;
- int[,] matA = new int[10,10];
- Console.Write("\nEnter the no. of row: ");
- i=Convert.ToInt16(Console.ReadLine());
- Console.Write("Enter the no. of col: ");
- j=Convert.ToInt16(Console.ReadLine());
- Console.WriteLine("\nEnter the Element in Matrix: ");
- for(row=0; row<i; row++)
- {
- for(col = 0; col<j; col++)
- {
- Console.Write("Matrix A[{0},{1}]: ", row, col);
- matA[row,col]= Convert.ToInt16(Console.ReadLine());
- }
- Console.WriteLine();
- }
- Console.WriteLine("\nMatrix A: ");
- for(row=0; row<i; row++)
- {
- for(col = 0; col<j; col++)
- Console.Write("{0:D2} ", matA[row,col]);
- Console.WriteLine();
- }
- Console.ReadLine();
- Console.WriteLine("\nLower triangle of Matrix A:");
- for(row=0;row<i; row++)
- {
- for(col=0;col<j; col++)
- {
- matA[row,col] = (row < col)? 0 : matA[row,col];
- Console.Write("{0} ",matA[row,col]);
- }
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- }
** Complete code
Logic:
- for(row=0;row<i; row++)
- {
- for(col=0;col<j; col++)
- {
- matA[row,col] = (row < col)? 0 : matA[row,col];
- Console.Write("{0} ",matA[row,col]);
- }
- Console.WriteLine();
Description:
In the above, if the number of column is greater than the row than 0 will be print other wise element of the matrix remain same as it is.
Screenshot:
Lower triangle of matrix |
Read Also: