c 语言计算球的表面积和体积
时间: 2024-06-12 18:10:53 浏览: 16
好的,计算球的表面积和体积可以使用以下公式:
球的表面积 = 4πr²
球的体积 = (4/3)πr³
其中,r为球的半径,π取3.14159。
下面是C语言代码实现:
```c
#include <stdio.h>
int main()
{
float r, s, v;
printf("请输入球的半径:");
scanf("%f", &r);
s = 4 * 3.14159 * r * r;
v = 4.0 / 3.0 * 3.14159 * r * r * r;
printf("球的表面积为:%f\n", s);
printf("球的体积为:%f\n", v);
return 0;
}
```
输入球的半径后,程序会计算出球的表面积和体积并输出。
相关问题
c 语言计算存款利息
计算存款利息的公式为:利息 = 本金 × 年利率 × 存款期限(年)。
以下是一个简单的 C 语言程序,可以计算存款利息:
```c
#include <stdio.h>
int main()
{
float principal, rate, time, interest;
printf("请输入本金:");
scanf("%f", &principal);
printf("请输入年利率:");
scanf("%f", &rate);
printf("请输入存款期限(年):");
scanf("%f", &time);
interest = principal * rate * time;
printf("利息为:%f\n", interest);
return 0;
}
```
在这个程序中,我们使用了 `float` 类型来存储本金、年利率、存款期限和利息。用户需要输入本金、年利率和存款期限,程序会计算出利息并输出。
c 语言计算字符长度
在C语言中,可以使用`strlen`函数来计算字符串长度,例如:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
int len = strlen(str);
printf("Length of str: %d\n", len);
return 0;
}
```
输出:
```
Length of str: 13
```
此外,如果要计算一个字符数组的长度,可以使用`sizeof`运算符,例如:
```c
#include <stdio.h>
int main() {
char arr[] = {'a', 'b', 'c', 'd', 'e'};
int len = sizeof(arr) / sizeof(char);
printf("Length of arr: %d\n", len);
return 0;
}
```
输出:
```
Length of arr: 5
```
相关推荐
![](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)