这里我们编写一个简单的 C 程序,根据用户提供的半径值计算圆的面积和周长。
公式:
面积 = 3.14 * 半径 * 半径周长 = 2 * 3.14 * 半径
根据用户输入计算面积和周长的程序
要计算面积和周长,我们必须知道圆的半径。程序将提示用户输入半径,并根据输入计算值。为简化起见,我们在程序中将标准PI值设为 3.14(常数)。其他详细信息在下面的示例程序中作为注释提及。
#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);}输出:
Enter radius of circle: 2Area of circle is: 12.560000Circumference of circle is: 12.560000
如果您想要更准确的结果,那么将 PI 值设为22/7而不是 3.14,它将为您提供面积和周长的精确值。
