In order to calculate the circumference and area of a circle, you need to obtain its radius, then calculate the circumference (2 π radius) and area (π * radius squared) according to the formula, and finally output the results.
How to use C language to output the circumference and area of a circle
In order to calculate and output the circumference and area of a circle area, you need to follow the following steps:
Include the necessary header files
#include <stdio.h> #include <math.h>
Define the radius of the circle
Declare a floating point variable to store the radius of the circle. For example:
float radius;
Get the radius of the circle input
Prompts the user to enter the radius of the circle and uses the scanf
function to store the input value. For example:
printf("请输入圆的半径:"); scanf("%f", &radius);
Calculate perimeter and area
Use the formula of a circle to calculate perimeter and area. The perimeter is 2 * π * radius
, and the area is π * radius squared
. For example:
float circumference = 2 * M_PI * radius; float area = M_PI * pow(radius, 2);
Output results
Use the printf
function to output the calculated perimeter and area. For example:
printf("周长:%.2f\n", circumference); printf("面积:%.2f\n", area);
Full code example:
#include <stdio.h> #include <math.h> int main() { float radius; printf("请输入圆的半径:"); scanf("%f", &radius); float circumference = 2 * M_PI * radius; float area = M_PI * pow(radius, 2); printf("周长:%.2f\n", circumference); printf("面积:%.2f\n", area); return 0; }
The above is the detailed content of How to output the circumference and area of a circle in C language. For more information, please follow other related articles on the PHP Chinese website!