Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C program to display only the lower triangle elements in a 3x3 2D array
In a 3x3 matrix, the lower triangle consists of elements where the row index is greater than or equal to the column index (i >= j). This includes the main diagonal and all elements below it.
Syntax
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
if(i >= j)
// Print element at array[i][j]
else
// Print space or skip
}
}
Visual Representation
Example 1: Display Lower Triangle
The following program displays only the lower triangle elements of a 3x3 matrix −
#include <stdio.h>
int main() {
int array[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j;
printf("Original Matrix:<br>");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", array[i][j]);
}
printf("<br>");
}
printf("\nLower Triangle:<br>");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(i >= j) {
printf("%d ", array[i][j]);
} else {
printf(" ");
}
}
printf("<br>");
}
return 0;
}
Original Matrix: 1 2 3 4 5 6 7 8 9 Lower Triangle: 1 4 5 7 8 9
Example 2: Display Upper Triangle
For comparison, here's how to display the upper triangle elements where i <= j −
#include <stdio.h>
int main() {
int array[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j;
printf("Original Matrix:<br>");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", array[i][j]);
}
printf("<br>");
}
printf("\nUpper Triangle:<br>");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(i <= j) {
printf("%d ", array[i][j]);
} else {
printf(" ");
}
}
printf("<br>");
}
return 0;
}
Original Matrix:
1 2 3
4 5 6
7 8 9
Upper Triangle:
1 2 3
5 6
9
Key Points
- Lower triangle condition: i >= j (row index greater than or equal to column index)
- Upper triangle condition: i <= j (row index less than or equal to column index)
- The main diagonal (where i == j) belongs to both triangles
- Use spaces or other characters to maintain matrix structure in output
Conclusion
Displaying triangle elements in a matrix is achieved by comparing row and column indices. The lower triangle includes elements where the row index is greater than or equal to the column index, making it useful for various mathematical operations and matrix manipulations.
Advertisements
