Home >Backend Development >C++ >How to express the circumference of a circle in C language

How to express the circumference of a circle in C language

下次还敢
下次还敢Original
2024-04-27 22:54:15699browse

The formula expressing the circumference of a circle in C language is: 2 π radius, where π is the pi ratio (approximate value 3.14159265), and radius is the radius of the circle. Sample code: define PI 3.14159265 (define π constant) float radius; (declare radius variable) scanf("%f", &radius); (read radius input) float circumference = 2 PI radius; (calculate circle circumference) printf(" The circumference of the circle is: %.2f\n", circumference); (print result)

How to express the circumference of a circle in C language

The representation of the circumference of the circle in C language

In C language, the circumference of a circle can be represented by the formula 2 * π * radius, where:

  • π is the pi ratio , is a constant with an approximate value of 3.14159265.
  • radius is the radius of the circle.

Sample code

The following C language code calculates and prints the circumference of a circle:

<code class="c">#include <stdio.h>
#define PI 3.14159265  // 定义 π 的近似值

int main() {
    float radius;

    printf("输入圆的半径:");
    scanf("%f", &radius);

    float circumference = 2 * PI * radius;

    printf("圆周长为:%.2f\n", circumference);

    return 0;
}</code>

In the code:

  • #define PI 3.14159265 Row definition PI is an approximate value of π.
  • float radius declares a floating point variable radius to store the radius.
  • scanf("%f", &radius) Reads the radius from user input.
  • float circumference = 2 * PI * radius Calculate the circumference of the circle and store it in the circumference variable.
  • printf("The circumference of the circle is: %.2f\n", circumference) Print the value of the circumference of the circle.

The above is the detailed content of How to express the circumference of a circle in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn