How to calculate the area and circumference of a circle in C language?

You can calculate the area and circumference of a circle using the following formula:

  1. The formula for area is: A = π * r * r, where A is the area, π is the constant pi (approximately equal to 3.14159), and r is the radius.
  2. Formula for circumference: C equals 2 times π times r
    where C is the circumference, π is the mathematical constant pi (approximately equal to 3.14159), and r is the radius.

Here is an example code in C language to calculate the area and perimeter of a circle.

#include <stdio.h>

#define PI 3.14159

int main() {
    double radius, area, circumference;

    // 从用户输入中获取半径
    printf("请输入圆的半径:");
    scanf("%lf", &radius);

    // 计算面积和周长
    area = PI * radius * radius;
    circumference = 2 * PI * radius;

    // 输出结果
    printf("圆的面积为:%lf\n", area);
    printf("圆的周长为:%lf\n", circumference);

    return 0;
}

Users need to input the radius of a circle, and the program will calculate and output the area and circumference of the circle based on the input radius. Please note that the code uses %lf to read and output values of type double.

bannerAds