C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to display the upper triangular matrixExplanationIn this program, we need to display the upper triangular matrix. Upper Triangular MatrixUpper triangular matrix is a square matrix in which all the elements below the principle diagonal are zero. To find the upper triangular matrix, a matrix needs to be a square matrix that is, the number of rows and columns in the matrix needs to be equal. Dimensions of a typical square matrix can be represented by n x n. Consider the above example, principle diagonal element of given matrix is (1, 6, 6). All the elements below diagonal needs to be zero to convert it into an upper triangular matrix, in our example, those elements are at positions (2,1), (3,1) and (3,2). To convert given matrix into the upper triangular matrix, loop through the matrix and set the values of the element to zero where row number is greater than column number. Algorithm
SolutionPython#Initialize matrix a a = [ [1, 2, 3], [8, 6, 4], [4, 5, 6] ]; #Calculates number of rows and columns present in given matrix rows = len(a); cols = len(a[0]); if(rows != cols): print("Matrix should be a square matrix"); else: #Performs required operation to convert given matrix into upper triangular matrix print("Upper triangular matrix: "); for i in range(0, rows): for j in range(0, cols): if(i > j): print("0"), else: print(a[i][j]), print(" "); Output: Upper triangular matrix: 1 2 3 0 6 4 0 0 6 C#include <stdio.h> int main() { int rows, cols; //Initialize matrix a int a[][3] = { {1, 2, 3}, {8, 6, 4}, {4, 5, 6} }; //Calculates number of rows and columns present in given matrix rows = (sizeof(a)/sizeof(a[0])); cols = (sizeof(a)/sizeof(a[0][0]))/rows; if(rows != cols){ printf("Matrix should be a square matrix\n"); } else{ //Performs required operation to convert given matrix into upper triangular matrix printf("Upper triangular matrix: \n"); for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(i > j) printf("0 "); else printf("%d ", a[i][j]); } printf("\n"); } } return 0; } Output: Upper triangular matrix: 1 2 3 0 6 4 0 0 6 JAVApublic class UpperTriangular { public static void main(String[] args) { int rows, cols; //Initialize matrix a int a[][] = { {1, 2, 3}, {8, 6, 4}, {4, 5, 6} }; //Calculates number of rows and columns present in given matrix rows = a.length; cols = a[0].length; if(rows != cols){ System.out.println("Matrix should be a square matrix"); } else { //Performs required operation to convert given matrix into upper triangular matrix System.out.println("Upper triangular matrix: "); for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(i > j) System.out.print("0 "); else System.out.print(a[i][j] + " "); } System.out.println(); } } } } Output: Upper triangular matrix: 1 2 3 0 6 4 0 0 6 C#using System; public class UpperTriangular { public static void Main() { int rows, cols; //Initialize matrix a int[,] a = { {1, 2, 3}, {8, 6, 4}, {4, 5, 6} }; //Calculates number of rows and columns present in given matrix rows = a.GetLength(0); cols = a.GetLength(1); if(rows != cols){ Console.WriteLine("Matrix should be a square matrix"); } else { //Performs required operation to convert given matrix into upper triangular matrix Console.WriteLine("Upper triangular matrix: "); for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(i > j) Console.Write("0 "); else Console.Write(a[i,j] + " "); } Console.WriteLine(); } } } } Output: Upper triangular matrix: 1 2 3 0 6 4 0 0 6 PHP<!DOCTYPE html> <html> <body> <?php //Initialize matrix a $a = array( array(1, 2, 3), array(8, 6, 4), array(4, 5, 6) ); //Calculates number of rows and columns present in given matrix $rows = count($a); $cols = count($a[0]); if($rows != $cols){ print("Matrix should be a square matrix<br>"); } else { //Performs required operation to convert given matrix into upper triangular matrix print("Upper triangular matrix: <br>"); for($i = 0; $i < $rows; $i++){ for($j = 0; $j < $cols; $j++){ if($i > $j) print("0 "); else print($a[$i][$j] . " "); } print("<br>"); } } ?> </body> </html> Output: Upper triangular matrix: 1 2 3 0 6 4 0 0 6
Next Topic#
|