C Language Program 09: Calculation of Radius, Diameter, Circumference, and Area of a Circle

The radius, diameter, circumference, and area of a circle are calculations frequently encountered in daily life.

Using the C language, we can design a program that only requires the user to input the radius to display the diameter, circumference, and area. Below is the program code:

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>  // System needs to be used for clearing the screen
#define PI 3.1415926 // Define Pi
int main() {    double radius, diameter, circumference, area;      // Define radius, diameter, circumference, and area, where radius can be a decimal    int enter;  // Define enter to receive getchar return value
    while (1) {        // Infinite loop, allowing the program to run indefinitely        printf("Please enter the radius of the circle:\n");
        // Check scanf return value to prevent invalid input        if (scanf("%lf", &radius) != 1)        // Condition check, it is the return value of scanf not equal to 1, not the input number not equal to 1, special attention!
        {            printf("Invalid input, please enter a number!\n\n");
            while ((enter = getchar()) != '\n' && enter != EOF);            // Clear input buffer, or wait for the user to press Enter            continue;  // Go back to the beginning of the loop        }
        if (radius <= 0) {            printf("Radius cannot be less than or equal to 0, please re-enter.\n\n");            // Clear buffer            while ((enter = getchar()) != '\n' && enter != EOF);            continue;        }
        // Calculate        diameter = radius * 2;        circumference = PI * diameter;        area = PI * radius * radius;
        // Output results        printf("Diameter: %.2lf\n", diameter);        printf("Circumference: %.2lf\n", circumference);        printf("Area: %.2lf\n", area);
        // Wait for the user to press Enter, then clear the screen        printf("\nPress Enter to continue...\n");        while ((enter = getchar()) != '\n' && enter != EOF);  // Clear buffer        (void)getchar();  // Wait for Enter, force ignore return value        system("cls");    // Clear screen    }
    return 0;}

Output display:

请输入圆的半径:8直径: 16.00周长: 50.27面积: 201.06
按回车键继续...
请输入圆的半径:10直径: 20.00周长: 62.83面积: 314.16
按回车键继续...
请输入圆的半径:-5半径不能小于或等于 0,请重新输入。
请输入圆的半径:

The formulas for the diameter, circumference, and area of a circle are basic concepts in elementary mathematics, and we just need to translate the formulas into C language. The following code uses an infinite loop while(1) to allow the program to run without exiting after one execution, and uses if condition to check if the radius is greater than 0.

while (1) {        // Infinite loop, allowing the program to run indefinitely        printf("Please enter the radius of the circle:\n");        // Check scanf return value to prevent invalid input        if (scanf("%lf", &radius) != 1)        // Condition check, it is the return value of scanf not equal to 1, not the input number not equal to 1, special attention!
        {            printf("Invalid input, please enter a number!\n\n");            while ((enter = getchar()) != '\n' && enter != EOF);            // Clear input buffer, or wait for the user to press Enter            continue;  // Go back to the beginning of the loop        }        if (radius <= 0) {            printf("Radius cannot be less than or equal to 0, please re-enter.\n\n");            // Clear buffer            while ((enter = getchar()) != '\n' && enter != EOF);            continue;        }

With the above code, the program becomes more interactive with the user, but it also makes the coding process more complicated. This is a typical case for learning while loops and condition checks.

Below is the simplest code:

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#define PI 3.1415926 // Define Pi
int main() {    double radius, diameter, circumference, area;      // Define radius, diameter, circumference, and area, where radius can be a decimal    printf("Please enter the radius:\n");    scanf("%lf", &radius);        // Calculate        diameter = radius * 2;        circumference = PI * diameter;        area = PI * radius * radius;
        // Output results        printf("Diameter: %.2lf\n", diameter);        printf("Circumference: %.2lf\n", circumference);        printf("Area: %.2lf\n", area);
    return 0;}

Leave a Comment