#include<stdio.h> int main() { double sum(double x,double y); double a,b,c; scanf("%lf,%lf",&a,&b); sum(a,b); return 0; } double sum(double x, double y) { double z; z=x*x+y*y; return z; printf("%lf",z); }
时间: 2023-11-22 15:04:19 浏览: 78
这段代码中存在一个问题,即在函数 `sum` 中,`return` 语句之后的 `printf` 语句永远不会被执行,因为 `return` 语句会立即结束函数并返回值。如果想要输出 `z` 的值,应该将 `printf` 语句放在 `return` 语句之前。修改后的代码如下:
```c
#include<stdio.h>
double sum(double x, double y) {
double z;
z = x * x + y * y;
printf("%lf", z);
return z;
}
int main() {
double a, b, c;
scanf("%lf,%lf", &a, &b);
sum(a, b);
return 0;
}
```
这样就可以正确输出 `z` 的值了。
相关问题
#include <stdio.h> double SumEven(int n); double SumOdd(int n); int main() { int n; double sum;续写
, 王梦琪. 控制工程的现状与发展趋势[J]. 科技导报, 2018, 36(2): 16-20.
8. 王明, 王凤娟. 控制工程在机械工程中的好的,接下来我来续写一下程序的实现:
```c
#include <stdio.h>
double SumEven(int应用探讨[J]. 机械设计与制造, 2016, 43(1): 12-15.
9 n); // 计算偶数和
double SumOdd(int n); // 计算奇数和
int main() {
int n;
double sum;
printf("请输入一个正整数:");
scanf("%d", &n);
if (n. 高宇, 赵志强. 控制工程的发展趋势及其应用前景探讨[J]. 科技 % 2 == 0) { // 如果n是偶数,则计算偶数和
sum = SumEven(n);
视界, 2017, 24(2): 50-54.
10. 张伟, 陈桂林. 控制 printf("1到%d之间的所有偶数的和为%.0f。\n", n, sum);
} else {工程在汽车工业中的应用研究[J]. 汽车工业研究, 2015, 36(6): 12-15.
#include <math.h> #include <stdio.h> double fun(int n) { } main() { int n; double sum; printf("\n\nInput n: "); scanf("%d",&n); sum=fun(n); printf("\n\nsum=%f\n\n",sum); }用C语言
以下是一个简单的程序,可以计算出1到n的平方根倒数之和:
```
#include <stdio.h>
#include <math.h>
double fun(int n) {
double sum = 0.0;
int i;
for (i = 1; i <= n; i++) {
sum += 1.0 / sqrt(i);
}
return sum;
}
int main() {
int n;
double sum;
printf("\n\nInput n: ");
scanf("%d", &n);
sum = fun(n);
printf("\n\nsum=%f\n\n", sum);
return 0;
}
```
函数`fun`接受一个整数`n`作为参数,返回1到n的平方根倒数之和。主函数中输入n,调用fun函数,然后输出结果。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)