用c语言fun函数输出点(m,n)到坐标原点的距离,小数点后必须保留2位有效数字(四舍五入),不足补零。
时间: 2023-06-03 08:08:11 浏览: 73
好的,这个问题可以回答。以下是代码实现:
```c
#include <stdio.h>
#include <math.h>
double fun(int m, int n) {
double distance = sqrt(pow(m, 2) + pow(n, 2)); // 计算距离
return round(distance * 100) / 100; // 四舍五入并保留两位小数
}
int main() {
int m = 3, n = 4;
double result = fun(m, n);
printf("(m,n)到坐标原点的距离为:%.2f", result);
return 0;
}
```
输出结果为:`(m,n)到坐标原点的距离为:5.00`。
相关问题
用c语言 输入华氏温度,转化为摄氏温度 后输出(四舍五入保留到小数点后2 位)。
您好,可以使用以下代码实现华氏温度转换为摄氏温度并输出:
```c
#include <stdio.h>
int main() {
double fahrenheit, celsius;
printf("请输入华氏温度:");
scanf("%lf", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("摄氏温度为:%.2lf\n", celsius);
return ;
}
```
感谢您的提问!
用C编写形如float fun(int a, int b)的函数,该函数的返回值是一个非负浮点数,是点(a,b)到坐标原点的距离。 在主函数main()中输入两个整数m和n。然后调用函数fun(int, int),并将m和n作为该函数的实参,最后输出该函数的返回值。小数点后必须保留2位有效数字(四舍五入),不足补零。
好的,我可以回答你的问题。以下是您所要求的代码:
```
#include <stdio.h>
#include <math.h>
float fun(int a, int b) {
float distance = sqrt(a*a + b*b);
return distance;
}
int main() {
int m, n;
scanf("%d %d", &m, &n);
printf("%.2f", fun(m, n));
return 0;
}
```
请注意,上述代码是用C语言编写的,该函数返回点(a, b)到坐标原点的距离,并在主函数中调用fun(int, int)函数并打印返回值。输出格式为小数点后两位。
相关推荐
![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)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)