C Online Compiler
Example: Print Lower Triangle of a Square Matrix in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Print Lower Triangle of a Square Matrix #include <stdio.h> int main() { // Step 1: Define the size of the square matrix int size = 3; // Step 2: Declare and initialize the square matrix int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; printf("Original Matrix:\n"); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } printf("\nLower Triangle of the Matrix:\n"); // Step 3: Iterate through rows for (int i = 0; i < size; i++) { // Step 4: Iterate through columns for (int j = 0; j < size; j++) { // Step 5: Check if the element is in the lower triangle if (j <= i) { printf("%d ", matrix[i][j]); // Print the element } else { printf(" "); // Print two spaces for alignment with two-digit numbers if any, or single space. } } printf("\n"); // Move to the next line after each row } return 0; }
Output
Clear
ADVERTISEMENTS