c语言编写程序输出两个数之和
时间: 2023-04-11 16:01:09 浏览: 256
可以的,以下是一个简单的 C 语言程序,可以输出两个数之和:
```
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
int sum = a + b;
printf("The sum of %d and %d is %d\n", a, b, sum);
return 0;
}
```
输出结果为:
```
The sum of 10 and 20 is 30
```
相关问题
用c语言编写程序比较两个数的大小
以下是用C语言编写程序比较两个数大小的示例代码:
```
#include <stdio.h>
int main() {
int a, b;
printf("请输入两个整数,用空格隔开:");
scanf("%d %d", &a, &b);
if (a > b) {
printf("%d 比 %d 大\n", a, b);
} else if (a < b) {
printf("%d 比 %d 小\n", a, b);
} else {
printf("%d 等于 %d\n", a, b);
}
return 0;
}
```
运行程序后,会提示用户输入两个整数,然后通过if语句分别判断两个数的大小关系,并输出相应的结果。如果a比b大,则输出"a 比 b 大";如果a比b小,则输出"a 比 b 小";如果a等于b,则输出"a 等于 b"。
C语言编写程序计算两个矩阵的和矩阵
当然可以!在C语言中,我们可以使用二维数组来表示矩阵,并编写程序计算两个矩阵的和矩阵。下面是一个简单的示例代码:
```c
#include <stdio.h>
#define ROWS 3
#define COLS 3
void addMatrix(int mat1[][COLS], int mat2[][COLS], int result[][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
void displayMatrix(int mat[][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main() {
int mat1[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int mat2[ROWS][COLS] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int result[ROWS][COLS];
addMatrix(mat1, mat2, result);
printf("Matrix 1:\n");
displayMatrix(mat1);
printf("\nMatrix 2:\n");
displayMatrix(mat2);
printf("\nSum Matrix:\n");
displayMatrix(result);
return 0;
}
```
在上面的代码中,我们定义了一个`addMatrix`函数,用于计算两个矩阵的和矩阵。然后,我们定义了一个`displayMatrix`函数,用于打印矩阵的内容。在`main`函数中,我们声明了两个矩阵`mat1`和`mat2`,并调用`addMatrix`函数计算它们的和矩阵,并使用`displayMatrix`函数打印出结果。
运行上述代码,将会输出以下结果:
```
Matrix 1:
1 2 3
4 5 6
7 8 9
Matrix 2:
9 8 7
6 5 4
3 2 1
Sum Matrix:
10 10 10
10 10 10
10 10 10
```
希望这个示例能够帮助到你!如果你有任何进一步的问题,请随时提问。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)