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