C++ Program to Print Lower Triangle of a Matrix

Write a C++ Program to Print Lower Triangle of a Matrix with an example. The following image shows you the lower triangle of a matrix.

C Program to find Lower Triangle Matrix 0

In this C++ matrix lower triangle example, we used nested for loops to iterate lowerTriMatrix matrix rows and columns. Within that loop, we used C++ If Else statement (if(rows >= columns)) to check whether the row value is greater than or equal to the column value. If True, print the matrix item at that position; otherwise, print 0 in that position.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Lower Triangle =  ";
	cin >> i >> j;
	
	int lowerTriMatrix[i][j];
	
	cout << "\nPlease Enter the Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> lowerTriMatrix[rows][columns];
		}		
	}

 	cout << "\nThe Result of the Lower Triangle Matrix is :\n";
 	for(rows = 0; rows < i; rows++)
  	{
  		cout << "\n";
   		for(columns = 0; columns < j; columns++)
    	{
    		if(rows >= columns)
    		{
    			cout << lowerTriMatrix[rows][columns] << " ";
			}
			else
			{
				cout << "0 ";
			}
   	 	}
  	}	

 	return 0;
}
C++ Program to Print Lower Triangle of a Matrix 1

In this C++ Matrix Lower Triangle example, we used some extra cout statements to show you the iteration-wise row, column values, and lowerTriMatrix[rows][columns] value.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Lower Triangle =  ";
	cin >> i >> j;
	
	int lowerTriMatrix[i][j];
	
	cout << "\nPlease Enter the Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> lowerTriMatrix[rows][columns];
		}		
	}

 	cout << "\nThe Result of the Lower Triangle Matrix is :\n";
 	for(rows = 0; rows < i; rows++)
  	{
  		cout << "\n";
   		cout << "\nRow Iteration = " << rows + 1 << ", Row Number = " << rows;
   		for(columns = 0; columns < j; columns++)
    	{
    		cout << "\nColumn Iteration = " << columns + 1 << ", Column Number = " << 
			  	columns << ", and Row Number = " << rows;
			cout << "\nThe result of (rows >= columns) = " << (rows >= columns) << 
				" and lowerTriMatrix[" << rows << "][" << columns << "] = " << lowerTriMatrix[rows][columns] << endl;
    		if(rows >= columns)
    		{
    			cout << lowerTriMatrix[rows][columns] << " ";
			}
			else
			{
				cout << "0 ";
			}
   	 	}
  	}	

 	return 0;
}
Please Enter Matrix rows and Columns to find Lower Triangle =  3 3

Please Enter the Matrix Items
1 2 3
4 5 6
7 8 9

The Result of the Lower Triangle Matrix is :


Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
The result of (rows >= columns) = 1 and lowerTriMatrix[0][0] = 1
1 
Column Iteration = 2, Column Number = 1, and Row Number = 0
The result of (rows >= columns) = 0 and lowerTriMatrix[0][1] = 2
0 
Column Iteration = 3, Column Number = 2, and Row Number = 0
The result of (rows >= columns) = 0 and lowerTriMatrix[0][2] = 3
0 

Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
The result of (rows >= columns) = 1 and lowerTriMatrix[1][0] = 4
4 
Column Iteration = 2, Column Number = 1, and Row Number = 1
The result of (rows >= columns) = 1 and lowerTriMatrix[1][1] = 5
5 
Column Iteration = 3, Column Number = 2, and Row Number = 1
The result of (rows >= columns) = 0 and lowerTriMatrix[1][2] = 6
0 

Row Iteration = 3, Row Number = 2
Column Iteration = 1, Column Number = 0, and Row Number = 2
The result of (rows >= columns) = 1 and lowerTriMatrix[2][0] = 7
7 
Column Iteration = 2, Column Number = 1, and Row Number = 2
The result of (rows >= columns) = 1 and lowerTriMatrix[2][1] = 8
8 
Column Iteration = 3, Column Number = 2, and Row Number = 2
The result of (rows >= columns) = 1 and lowerTriMatrix[2][2] = 9
9

C++ Program to Print Lower Triangle of a Matrix using a While Loop

#include<iostream>
using namespace std;

int main()
{
	int size, i, j, rows, columns;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Lower Triangle =  ";
	cin >> i >> j;
	
	int lowerTriMatrix[i][j];
	
	cout << "\nPlease Enter the Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> lowerTriMatrix[rows][columns];
		}		
	}

 	cout << "\nThe Result of the Lower Triangle Matrix is :\n";
 	rows = 0; 
 	while(rows < i)
  	{
  		cout << "\n";
  		columns = 0; 
   		while(columns < j)
    	{
    		if(rows >= columns)
    		{
    			cout << lowerTriMatrix[rows][columns] << " ";
			}
			else
			{
				cout << "0 ";
			}
			columns++;
   	 	}
   	 	rows++;
  	}	

 	return 0;
}
Please Enter Matrix rows and Columns to find Lower Triangle =  3 3

Please Enter the Matrix Items
1 2 3
9 8 7
4 7 6

The Result of the Lower Triangle Matrix is :

1 0 0 
9 8 0 
4 7 6 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.