# Program to print Lower triangular and Upper triangular matrix of an array

Given a two dimensional array, Write a program to print lower triangular matrix and upper triangular matrix.

• Lower triangular matrix is a matrix which contain elements below principle diagonal including principle diagonal elements and rest of the elements are 0.
• Upper triangular matrix is a matrix which contain elements above principle diagonal including principle diagonal elements and rest of the elements are 0.

Examples:

```Input : matrix[3][3] = {1 2 3
4 5 6
7 8 9}
Output :
Lower : 1 0 0        Upper : 1 2 3
4 5 0                0 5 6
7 8 9                0 0 9

Input : matrix[3][3] = {7 8 9
3 2 1
6 5 4}
Output :
Lower : 7 0 0       Upper : 7 8 9
3 2 0               0 2 1
6 5 4               0 0 4
```

Steps:

1. For lower triangular matrix, we check the index position i and j i.e row and column respectively. If column position is greater than row position we simply make that position 0.
2. For upper triangular matrix, we check the index position i and j i.e row and column respectively. If column position is smaller than row position we simply make that position 0.
`// CPP program to print Lower `
`// triangular and Upper triangular`
`// matrix of an array`
`#include<iostream>`
`using` `namespace` `std;`
`// Function to form lower triangular matrix`
`void` `lower(``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)`
`            ``{`
`                ``matrix[i][j] = 0;`
`            ``}`
`            ``cout << matrix[i][j] << ``" "``;`
`        ``}`
`        ``cout << endl;`
`    ``}`
`}`
`// 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)`
`            ``{`
`                ``matrix[i][j] = 0;`
`            ``}`
`            ``cout << matrix[i][j] << ``" "``;`
`        ``}`
`        ``cout << endl;`
`    ``}`
`}`
`// Driver function`
`int` `main()`
`{`
`    ``int` `matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};`
`    ``int` `row = 3, col = 3;`
`    `
`    ``cout << ``"Lower triangular matrix: \n"``;`
`    ``lower(matrix, row, col);`
`    `
`    ``cout << ``"Upper triangular matrix: \n"``;`
`    ``upper(matrix, row, col);`
`        `
`    ``return` `0;`
`}`

Output:

```Lower triangular matrix:
1 0 0
4 5 0
7 8 9
Upper triangular matrix:
1 0 0
0 5 0
0 0 9

```

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits:http://www.geeksforgeeks.org/program-print-lower-triangular-upper-triangular-matrix-array/
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at