C Online Compiler
Example: Display Lower Triangular Matrix in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Display Lower Triangular Matrix #include <stdio.h> int main() { // Step 1: Declare and initialize the square matrix int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int rows = 3; int cols = 3; // Step 2: Display the original matrix for comparison printf("Original Matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d\t", matrix[i][j]); } printf("\n"); } // Step 3: Display the lower triangular matrix printf("\nLower Triangular Matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { // If the current element is on or below the main diagonal (i >= j) if (i >= j) { printf("%d\t", matrix[i][j]); } else { // Otherwise, print '0' for elements above the main diagonal printf("0\t"); } } printf("\n"); } return 0; }
Output
Clear
ADVERTISEMENTS