Write a C++ Program to Print Upper Triangle of a Matrix with an example. The following image shows you the matrix upper triangle.
In this C++ matrix upper triangle example, we used nested for loops to iterate upperTriMatrix matrix rows and columns. Within that, we used C++ If Else statement (if(columns >= rows)) to check whether the column value is greater than or equal to the row value. If True, print the upperTriMatrix 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 Upper Triangle = "; cin >> i >> j; int upperTriMatrix[i][j]; cout << "\nPlease Enter the Matrix Items\n"; for(rows = 0; rows < i; rows++) { for(columns = 0; columns < i; columns++) { cin >> upperTriMatrix[rows][columns]; } } cout << "\nThe Result of the Upper Triangle Matrix is :\n"; for(rows = 0; rows < i; rows++) { cout << "\n"; for(columns = 0; columns < j; columns++) { if(columns >= rows) { cout << upperTriMatrix[rows][columns] << " "; } else { cout << "0 "; } } } return 0; }
In this C++ Matrix Upper Triangle example, we used extra cout statements to show the iteration-wise row value, column value, and upperTriMatrix[rows][columns] value.
#include<iostream> using namespace std; int main() { int i, j, rows, columns; cout << "\nPlease Enter Matrix rows and Columns to find Upper Triangle = "; cin >> i >> j; int upperTriMatrix[i][j]; cout << "\nPlease Enter the Matrix Items\n"; for(rows = 0; rows < i; rows++) { for(columns = 0; columns < i; columns++) { cin >> upperTriMatrix[rows][columns]; } } cout << "\nThe Result of the Upper 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 (columns >= rows) = " << (columns >= rows) << " and upperTriMatrix[" << rows << "][" << columns << "] = " << upperTriMatrix[rows][columns] << endl; if(columns >= rows) { cout << upperTriMatrix[rows][columns] << " "; } else { cout << "0 "; } } } return 0; }
Please Enter Matrix rows and Columns to find Upper Triangle = 2 2
Please Enter the Matrix Items
10 20
30 40
The Result of the Upper Triangle Matrix is :
Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
The result of (columns >= rows) = 1 and upperTriMatrix[0][0] = 10
10
Column Iteration = 2, Column Number = 1, and Row Number = 0
The result of (columns >= rows) = 1 and upperTriMatrix[0][1] = 20
20
Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
The result of (columns >= rows) = 0 and upperTriMatrix[1][0] = 30
0
Column Iteration = 2, Column Number = 1, and Row Number = 1
The result of (columns >= rows) = 1 and upperTriMatrix[1][1] = 40
40
C++ Program to Print Upper Triangle of a Matrix using a While Loop
#include<iostream> using namespace std; int main() { int i, j, rows, columns; cout << "\nPlease Enter Matrix rows and Columns to find Upper Triangle = "; cin >> i >> j; int upperTriMatrix[i][j]; cout << "\nPlease Enter the Matrix Items\n"; for(rows = 0; rows < i; rows++) { for(columns = 0; columns < i; columns++) { cin >> upperTriMatrix[rows][columns]; } } cout << "\nThe Result of the Upper Triangle Matrix is :\n"; rows = 0; while(rows < i) { cout << "\n"; columns = 0; while(columns < j) { if(columns >= rows) { cout << upperTriMatrix[rows][columns] << " "; } else { cout << "0 "; } columns++; } rows++; } return 0; }
Please Enter Matrix rows and Columns to find Upper Triangle = 3 3
Please Enter the Matrix Items
11 22 33
44 55 66
77 88 99
The Result of the Upper Triangle Matrix is :
11 22 33
0 55 66
0 0 99