编写以下功能函数 (1)求两个数的和 (2)求两个数的差 (3)求两个数的积 (4)求两个数的商
时间: 2024-06-14 20:03:54 浏览: 42
以下是对应功能函数的C代码演示:
1.求两个数的和
```c
#include <stdio.h>
void sum_dif(int n1, int n2, int *sum) {
*sum = n1 + n2;
}
int main(void) {
int na, nb, sum = 0;
printf("请输入两个值:");
scanf("%d%d", &na, &nb);
printf("na=%d,nb=%d\n", na, nb);
sum_dif(na, nb, &sum);
printf("sum=%d\n", sum);
return 0;
}
```
2.求两个数的差
```c
#include <stdio.h>
void sum_dif(int n1, int n2, int *dif) {
*dif = n1 - n2;
}
int main(void) {
int na, nb, dif = 0;
printf("请输入两个值:");
scanf("%d%d", &na, &nb);
printf("na=%d,nb=%d\n", na, nb);
sum_dif(na, nb, &dif);
printf("dif=%d\n", dif);
return 0;
}
```
3.求两个数的积
```c
#include <stdio.h>
void product(int n1, int n2, int *pro) {
*pro = n1 * n2;
}
int main(void) {
int na, nb, pro = 0;
printf("请输入两个值:");
scanf("%d%d", &na, &nb);
printf("na=%d,nb=%d\n", na, nb);
product(na, nb, &pro);
printf("product=%d\n", pro);
return 0;
}
```
4.求两个数的商
```c
#include <stdio.h>
void division(int n1, int n2, float *div) {
*div = (float)n1 / n2;
}
int main(void) {
int na, nb;
float div = 0.0;
printf("请输入两个值:");
scanf("%d%d", &na, &nb);
printf("na=%d,nb=%d\n", na, nb);
division(na, nb, &div);
printf("division=%f\n", div);
return 0;
}
```
阅读全文