c语言计算长方形的周长和面积
时间: 2024-09-26 15:16:21 浏览: 121
在C语言中,计算长方形的周长和面积通常涉及到两个变量,分别代表长(length)和宽(width)。下面是一个简单的函数示例,用于计算长方形的周长和面积:
```c
#include <stdio.h>
// 函数声明
void calculate_rectangle(int length, int width);
int main() {
int length, width;
// 用户输入长和宽
printf("请输入长方形的长度:");
scanf("%d", &length);
printf("请输入长方形的宽度:");
scanf("%d", &width);
// 调用函数计算并输出结果
calculate_rectangle(length, width);
return 0;
}
// 计算函数
void calculate_rectangle(int length, int width) {
int perimeter; // 周长
int area; // 面积
perimeter = 2 * (length + width); // 周长公式:2*(长+宽)
area = length * width; // 面积公式:长*宽
printf("长方形的周长为:%d\n", perimeter);
printf("长方形的面积为:%d\n", area);
}
```
在这个程序中,用户首先输入长方形的长度和宽度,然后`calculate_rectangle`函数会根据输入计算并打印出周长和面积。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)