用C语言编写程序,调用函数求一个圆柱体的表面积和体积
时间: 2023-08-21 16:06:37 浏览: 85
好的,以下是用C语言编写程序,调用函数求一个圆柱体的表面积和体积的代码:
```c
#include <stdio.h>
// 定义求圆柱体表面积的函数
float surface_area(float r, float h) {
float area = 2 * 3.14 * r * (r + h);
return area;
}
// 定义求圆柱体体积的函数
float volume(float r, float h) {
float v = 3.14 * r * r * h;
return v;
}
int main() {
float radius, height;
printf("请输入圆柱体的底面半径和高度:");
scanf("%f%f", &radius, &height);
float s = surface_area(radius, height);
float v = volume(radius, height);
printf("圆柱体的表面积为:%.2f\n", s);
printf("圆柱体的体积为:%.2f\n", v);
return 0;
}
```
运行程序后,输入圆柱体的底面半径和高度,程序会调用 surface_area 和 volume 函数分别计算表面积和体积,并将结果输出。注意:这里使用了 3.14 来近似表示圆周率。
相关问题
c语言编写程序,调用函数求一个圆柱体的表面积和体积
好的,计算圆柱体的表面积和体积需要使用圆柱体的半径和高,可以使用以下公式进行计算:
圆柱体的表面积 = 2 * π * r * h + 2 * π * r^2
圆柱体的体积 = π * r^2 * h
其中,π是一个常数,约等于3.14159。
现在我们可以编写一个名为cylinder()的函数,该函数的参数为圆柱体的半径和高,返回值为一个包含表面积和体积的结构体。具体实现如下:
```c
#include <stdio.h>
#define PI 3.14159
struct Cylinder {
double surface_area;
double volume;
};
struct Cylinder cylinder(double r, double h) {
struct Cylinder result;
result.surface_area = 2 * PI * r * h + 2 * PI * r * r;
result.volume = PI * r * r * h;
return result;
}
int main() {
double r = 3.0, h = 5.0;
struct Cylinder c = cylinder(r, h);
printf("The surface area of the cylinder is: %.2f\n", c.surface_area);
printf("The volume of the cylinder is: %.2f\n", c.volume);
return 0;
}
```
该程序中使用了一个结构体Cylinder来保存圆柱体的表面积和体积,同时定义了一个常量PI来表示π的值。在函数cylinder()中,根据上述公式计算圆柱体的表面积和体积,并将结果保存在一个类型为Cylinder的结构体中,最后将该结构体作为函数的返回值。在主函数中,调用cylinder()函数,并输出计算结果。
运行该程序,可以得到以下输出结果:
```
The surface area of the cylinder is: 150.80
The volume of the cylinder is: 141.37
```
表面积保留两位小数,体积也保留两位小数。
用c语言调用函数求一个圆柱体的表面积和体积
以下是使用C语言调用函数求一个圆柱体的表面积和体积的示例代码:
```c
#include <stdio.h>
#define Pi 3.1415926
void fun(int h, int r) {
float area, volume;
area = 2 * Pi * r * r + 2 * Pi * r;
volume = Pi * r * r * h;
printf("The surface area of this cylinder is %f, the volume is %f.", area, volume);
}
int main(void) {
int h, r;
printf("Please enter the height and radius of the cylinder:");
scanf("%d%d", &h, &r);
fun(h, r);
return 0;
}
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![cpp](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)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)