Prerequisite – Multidimensional Arrays in C / C++
Given a two dimensional array, Write a program to print lower triangular matrix and upper triangular matrix.
- Lower triangular matrix is a matrix which contains elements below principal diagonal including principal diagonal elements and rest of the elements are 0.
- Upper triangular matrix is a matrix which contains elements above principal diagonal including principal diagonal elements and rest of the elements are 0.
LOWER TRIANGULAR :

UPPER TRIANGULAR :

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:
- 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.
- 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.
C++
#include<iostream>
using namespace std;
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)
{
cout << "0" << " " ;
}
else
cout << matrix[i][j] << " " ;
}
cout << endl;
}
}
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;
}
}
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;
}
|
C
#include <stdio.h>
void lower ( int a[3][3], int r, int c)
{
for ( int i = 0; i < r; i++)
{
for ( int j = 0; j < c; j++)
{
if (i > j)
printf ( "0" );
else
printf ( "%d" , a[i][j]);
printf ( " " );
}
printf ( "\n" );
}
}
void upper ( int a[3][3], int r, int c)
{
for ( int i = 0; i < r; i++)
{
for ( int j = 0; j < c; j++)
{
if (i < j)
printf ( "0" );
else
printf ( "%d" , a[i][j]);
printf ( " " );
}
printf ( "\n" );
}
}
int main() {
int r = 3, c = 3;
int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf ( "\nLower Triangular Matrix is :\n" );
lower(a, r, c);
printf ( "\nUpper Triangular Matrix is :\n" );
upper(a, r, c);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static void lower( 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();
}
}
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();
}
}
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( "Lower triangular matrix: " );
lower(matrix, row, col);
System.out.println( "Upper triangular matrix: " );
upper(matrix, row, col);
}
}
|
Python3
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 ( " " );
def upper(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 ( "Lower triangular matrix: " );
lower(matrix, row, col);
print ( "Upper triangular matrix: " );
upper(matrix, row, col);
|
C#
using System;
class GFG
{
static void lower( 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 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( "Lower triangular matrix: " );
lower(matrix, row, col);
Console.WriteLine( "Upper triangular matrix: " );
upper(matrix, row, col);
}
}
|
PHP
<?php
function lower( $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" ;
}
}
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" ;
}
}
$matrix = array ( array (1, 2, 3),
array (4, 5, 6),
array (7, 8, 9));
$row = 3; $col = 3;
echo "Lower triangular matrix: \n" ;
lower( $matrix , $row , $col );
echo "Upper triangular matrix: \n" ;
upper( $matrix , $row , $col );
?>
|
Javascript
<script>
function lower( matrix,row,col)
{
let i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i < j)
{
document.write( "0" + " " );
}
else
document.write(matrix[i][j] + " " );
}
document.write( "<br>" );
}
}
function upper(matrix,row,col)
{
let i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i > j)
{
document.write( "0" + " " );
}
else
document.write(matrix[i][j] + " " );
}
document.write( "<br>" );
}
}
let matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]];
let row = 3, col = 3;
document.write( "Lower triangular matrix: <br>" );
lower(matrix, row, col);
document.write( "Upper triangular matrix: <br>" );
upper(matrix, row, col);
</script>
|
Output
Lower triangular matrix:
1 0 0
4 5 0
7 8 9
Upper triangular matrix:
1 2 3
0 5 6
0 0 9
Time Complexity: O(row x col)
Auxiliary Space: O(1), since no extra space has been taken.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!