用c语言求两个数的和差积商
时间: 2023-05-11 13:06:06 浏览: 235
可以使用以下代码来求两个数的和、差、积和商:
#include <stdio.h>
int main() {
int a, b;
printf("请输入两个整数:\n");
scanf("%d%d", &a, &b);
printf("%d + %d = %d\n", a, b, a + b);
printf("%d - %d = %d\n", a, b, a - b);
printf("%d * %d = %d\n", a, b, a * b);
if (b != 0) {
printf("%d / %d = %d\n", a, b, a / b);
} else {
printf("除数不能为0!\n");
}
return 0;
}
相关问题
C语言求两个数的和商 差积
```c
#include<stdio.h>
int main()
{
int a,b,sum,sub,mul;
float div; //注意商需要为浮点型
printf("请输入两个整数:\n");
scanf("%d%d",&a,&b);
sum=a+b;
sub=a-b;
mul=a*b;
div=(float)a/b; //需要先强制类型转换为浮点型,保证精度
printf("它们的和为:%d\n",sum);
printf("它们的差为:%d\n",sub);
printf("它们的积为:%d\n",mul);
printf("它们的商为:%.2f\n",div); //保留两位小数
return 0;
}
```
c语言输入两个整数求他们的和差积商
好的,下面是一个实现输入两个整数求和、差、积、商的C语言代码示例:
```
#include <stdio.h>
int main()
{
int num1, num2, sum, difference, product;
float quotient;
printf("请输入两个整数:\n");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
difference = num1 - num2;
product = num1 * num2;
quotient = (float)num1 / num2; // 注意转换为浮点数
printf("它们的和是:%d\n", sum);
printf("它们的差是:%d\n", difference);
printf("它们的积是:%d\n", product);
printf("它们的商是:%f\n", quotient);
return 0;
}
```
在上述代码中,我们使用了四个变量来存储输入的两个整数的和、差、积和商。其中,商需要用浮点数类型来存储,并且需要进行类型转换,以保证计算的精度。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)