自定义4个函数求两个数的和差积商。用c语言编写在主函数中调用函数求a+b,a-b,a*b,a/b.a/b。
时间: 2024-03-01 09:54:03 浏览: 20
好的,我会为您编写这些函数。以下是代码:
```c
#include <stdio.h>
float add(float a, float b) {
return a + b;
}
float subtract(float a, float b) {
return a - b;
}
float multiply(float a, float b) {
return a * b;
}
float divide(float a, float b) {
return a / b;
}
int main() {
float a, b;
printf("请输入两个数:");
scanf("%f %f", &a, &b);
printf("%.2f + %.2f = %.2f\n", a, b, add(a, b));
printf("%.2f - %.2f = %.2f\n", a, b, subtract(a, b));
printf("%.2f * %.2f = %.2f\n", a, b, multiply(a, b));
printf("%.2f / %.2f = %.2f\n", a, b, divide(a, b));
return 0;
}
```
您可以将此代码复制到您的编译器中运行。输入两个数后,程序将使用四个自定义函数计算它们的和、差、积和商,并将结果打印到屏幕上。
相关问题
自定义4个函数求两个数的和差积商。在主函数中调用函数求a+b,a-b,a×b,a÷b用c语言
好的,那么这是一份 C 语言的代码实现:
```c
#include <stdio.h>
// 求和
double sum(double a, double b) {
return a + b;
}
// 求差
double difference(double a, double b) {
return a - b;
}
// 求积
double product(double a, double b) {
return a * b;
}
// 求商
double quotient(double a, double b) {
return a / b;
}
int main() {
double a = 10, b = 3;
// 求和
double s = sum(a, b);
printf("a + b = %.2lf\n", s);
// 求差
double d = difference(a, b);
printf("a - b = %.2lf\n", d);
// 求积
double p = product(a, b);
printf("a * b = %.2lf\n", p);
// 求商
double q = quotient(a, b);
printf("a / b = %.2lf\n", q);
return 0;
}
```
这里同样定义了四个函数:`sum`、`difference`、`product` 和 `quotient`,分别用于求两个数的和、差、积和商。在主函数中,我们调用这些函数,传入参数 a 和 b,得到相应的结果并输出。需要注意的是,C 语言中的 `printf` 函数需要使用格式化输出,我们使用 `%.2lf` 来输出一个双精度浮点数并保留两位小数。
用c语言自定义4个函数求两个数的和差商积。在主函数中调用函数求a+b,a-b,a*b,a/b
好的,以下是用C语言自定义4个函数求两个数的和差商积的代码:
```c
#include <stdio.h>
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float b);
int main() {
float a = 10.0, b = 2.0;
printf("a + b = %.2f\n", add(a, b));
printf("a - b = %.2f\n", subtract(a, b));
printf("a * b = %.2f\n", multiply(a, b));
printf("a / b = %.2f\n", divide(a, b));
return 0;
}
float add(float a, float b) {
return a + b;
}
float subtract(float a, float b) {
return a - b;
}
float multiply(float a, float b) {
return a * b;
}
float divide(float a, float b) {
return a / b;
}
```
运行结果:
```
a + b = 12.00
a - b = 8.00
a * b = 20.00
a / b = 5.00
```
其中,`add`函数用于求两个数的和,`subtract`函数用于求两个数的差,`multiply`函数用于求两个数的积,`divide`函数用于求两个数的商。在主函数中分别调用这四个函数即可求出所需的结果。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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)