Home >Backend Development >C++ >How to output the circumference and area of a circle in C language
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
<code class="c">#include <stdio.h> #include <math.h></code>
Define the radius of the circle
Declare a floating point variable to store the radius of the circle. For example:
<code class="c">float radius;</code>
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:
<code class="c">printf("请输入圆的半径:"); scanf("%f", &radius);</code>
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:
<code class="c">float circumference = 2 * M_PI * radius; float area = M_PI * pow(radius, 2);</code>
Output results
Use the printf
function to output the calculated perimeter and area. For example:
<code class="c">printf("周长:%.2f\n", circumference); printf("面积:%.2f\n", area);</code>
Full code example:
<code class="c">#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; }</code>
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!