企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# C 程序:计算圆的面积和周长 > 原文: [https://beginnersbook.com/2014/06/c-program-to-calculate-area-and-circumference-of-circle/](https://beginnersbook.com/2014/06/c-program-to-calculate-area-and-circumference-of-circle/) 这里我们编写一个简单的 C 程序,根据用户提供的半径值计算圆的面积和周长。 公式: ```c 面积 = 3.14 * 半径 * 半径 周长 = 2 * 3.14 * 半径 ``` ## 根据用户输入计算面积和周长的程序 要计算面积和周长,我们必须知道圆的半径。程序将提示用户输入半径,并根据输入计算值。为简化起见,我们在程序中将标准`PI`值设为 3.14(常数)。其他详细信息在下面的示例程序中作为注释提及。 ```c #include <stdio.h> int main() { int circle_radius; float PI_VALUE=3.14, circle_area, circle_circumf; //Ask user to enter the radius of circle printf("\nEnter radius of circle: "); //Storing the user input into variable circle_radius scanf("%d",&circle_radius); //Calculate and display Area circle_area = PI_VALUE * circle_radius * circle_radius; printf("\nArea of circle is: %f",circle_area); //Caluclate and display Circumference circle_circumf = 2 * PI_VALUE * circle_radius; printf("\nCircumference of circle is: %f",circle_circumf); return(0); } ``` 输出: ```c Enter radius of circle: 2 Area of circle is: 12.560000 Circumference of circle is: 12.560000 ``` 如果您想要更准确的结果,那么将 PI 值设为`22/7`而不是 3.14,它将为您提供面积和周长的精确值。