C语言:求矩阵下三角形元素之和。
时间: 2023-06-06 11:05:43 浏览: 97
可以的,对于一个矩阵,其下三角形元素就是其主对角线以下的所有元素,可以通过两层循环来实现求和,具体代码实现可以参考以下:
```c
#include <stdio.h>
void main(){
int m[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int sum = 0;
for(int i=0; i<3; i++){
for(int j=0; j<=i; j++){
sum += m[i][j];
}
}
printf("下三角形元素之和为:%d", sum);
}
```
运行结果为:下三角形元素之和为:45
相关问题
c语言蛇形矩阵上三角形
以下是C语言蛇形矩阵上三角形的代码和输出结果:
```c
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a = 1, b = 1, t;
for (int i = 1; i <= n; i++) {
b = a;
a += i;
t = i + 1;
for (int j = i; j <= n; j++) {
printf("%d ", b);
b += t;
t++;
}
printf("\n");
}
return 0;
}
```
输出结果为:
```
2 1 3 6 10 15 21
2 5 9 14 20
4 8 13 19
7 12 18
11 17
16
```
用矩阵输出杨辉三角形用c语言
下面是用C语言输出杨辉三角形的代码:
```c
#include <stdio.h>
int main() {
int n, i, j, coef = 1;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0) {
coef = 1;
} else {
coef = coef * (i - j + 1) / j;
}
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
```
运行程序后,输入要输出的行数,即可得到输出的杨辉三角形。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)