C语言程序:输出矩阵及其上下三角矩阵
#include <stdio.h>
int main() { int n, i, j; printf("Input n:"); scanf("%d", &n); int matrix[n][n];
printf("Input the elements of the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("The input matrix is:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", matrix[i][j]);
}
printf("\n");
}
printf("The lower triangular matrix is:\n");
for (i = 0; i < n; i++) {
for (j = 0; j <= n; j++) {
if (i >= j)
printf("%4d", matrix[i][j]);
else
printf(" ");
}
printf("\n");
}
printf("The upper triangular matrix is:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i <= j)
printf("%4d", matrix[i][j]);
else
printf(" ");
}
printf("\n");
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pfKe 著作权归作者所有。请勿转载和采集!