Q. Write a program to print Upper triangular matrix of an array.
Upper triangular matrix :- A square matrix is called upper triangular if all the entries below the main diagonal are zero.
For Example :-
Input = matrix[3][3] =
{1 2 3
4 5 6
7 8 9}
Output = Lower :
1 2 3
0 5 6
0 0 9
Upper Triangular Matrix Algorithm
START Step 1 → Find the index position of row and column. Step 2 → If column position is smaller than row position then make that position 0. STOP
Upper Triangular Matrix Program
#include <stdio.h> int main() { int rows=3, cols=3, i, j, matrix[3][3]={1,2,3,4,5,6,7,8,9}; printf("The Upper Triangular Matrix is: "); for(i = 0; i < rows; i++){ printf("\n"); for(j = 0; j < cols; j++){ if(i > j){ printf("0"); printf("\t"); } else{ printf("%d\t ", matrix[i][j]); } } } return 0; }
#include <iostream> using namespace std; // Function to form upper triangular marix void upper(int matrix[3][3], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i > j) { cout << "0" << " "; } else cout << matrix[i][j] << " "; } cout << endl; } } // Driver Code int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int row = 3, col = 3; cout << "The Upper Triangular Matrix is: \n"; upper(matrix, row, col); return 0; }
class LFC { // Method to form upper // triangular matrix static void upper(int matrix[][], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i > j) { System.out.print("0" + " "); } else System.out.print(matrix[i][j] + " "); } System.out.println(); } } // Driver Code public static void main(String args[]) { int matrix[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int row = 3, col = 3; System.out.println("The Upper Triangular Matrix is: "); upper(matrix, row, col); } }
def lower(matrix, row, col): for i in range(0, row): for j in range(0, col): if (i < j): print("0", end = " "); else: print(matrix[i][j], end = " " ); print(" "); matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; row = 3; col = 3; print("The Lower Triangular Matrix is: "); lower(matrix, row, col);
using System; class LFC { // Method to form upper // triangular matrix static void upper(int [,]matrix, int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i > j) { Console.Write("0" + " "); } else Console.Write(matrix[i, j] + " "); } Console.WriteLine(); } } static public void Main () { int [,]matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int row = 3, col = 3; Console.WriteLine("The Upper Triangular Matrix is: "); upper(matrix, row, col); } }
// Function to form // upper triangular marix function upper($matrix, $row, $col) { $i; $j; for ($i = 0; $i < $row; $i++) { for ($j = 0; $j < $col; $j++) { if ($i > $j) { echo "0" , " "; } else echo $matrix[$i][$j] ," "; } echo "\n"; } } // Driver Code $matrix = array (array (1, 2, 3), array (4, 5, 6), array (7, 8, 9)); $row = 3; $col = 3; echo "The Upper Triangular Matrix is: \n"; upper($matrix, $row, $col);
#include <stdio.h> int main() { int rows=3, cols=3, i, j, matrix[3][3]={1,2,3,4,5,6,7,8,9}; printf("The Upper Triangular Matrix is: "); for(i = 0; i < rows; i++){ printf("\n"); for(j = 0; j < cols; j++){ if(i > j){ printf("0"); printf("\t"); } else{ printf("%d\t ", matrix[i][j]); } } } return 0; }
Output
The Upper Triangular Matrix is: 1 2 3 0 5 6 0 0 9
Recommended Programs
Program to print lower triangular matrix.Program to add two matrices.