APC-S-009.c (1456B)
1 /* Write a program to find the upper and lower triangular matrix. */ 2 /* Author: Amit Dutta, Date: 18-11-2025 */ 3 4 #include <stdio.h> 5 6 int main() 7 { 8 int i, j, rows, cols; 9 printf("\nEnter the number of rows and columns : "); 10 scanf("%d %d", &rows, &cols); 11 12 if (rows != cols) 13 { 14 printf("Triangular matrix definitions only apply to square matrices (rows == columns).\n"); 15 return 1; 16 } 17 18 int matrix[rows][cols]; 19 20 printf("Enter the elements of matrix (%d x %d): \n", rows, cols); 21 for (i = 0; i < rows; i++) 22 for (j = 0; j < cols; j++) 23 { 24 printf("Position %d%d: ", i, j); 25 scanf("%d", &matrix[i][j]); 26 } 27 printf("\nMatrix: \n"); 28 for (i = 0; i < rows; i++) 29 { 30 for (j = 0; j < cols; j++) 31 printf("%d ", matrix[i][j]); 32 printf("\n"); 33 } 34 35 printf("\nUpper triangular of the Matrix: \n"); 36 for (i = 0; i < rows; i++) 37 { 38 for (j = 0; j < cols; j++) 39 { 40 if (j >= i) 41 printf("%d ", matrix[i][j]); 42 else 43 printf("~ "); 44 } 45 printf("\n"); 46 } 47 48 printf("\nLower triangular of the Matrix: \n"); 49 for (i = 0; i < rows; i++) 50 { 51 for (j = 0; j < cols; j++) 52 { 53 if (j <= i) 54 printf("%d ", matrix[i][j]); 55 else 56 printf("~ "); 57 } 58 printf("\n"); 59 } 60 61 return 0; 62 }