C program to calculate and identify the area of a circle

Simple C program that calculates and identifies the area of a circle when you provide the radius as input:

#include <stdio.h>

int main() {
    // Declare variables
    double radius, area;

    // Ask the user to enter the radius
    printf("Enter the radius of the circle: ");
    scanf("%lf", &radius);

    // Calculate the area of the circle using the formula: π * r * r
    area = 3.14159265359 * radius * radius;

    // Display the result
    printf("The area of the circle with radius %.2lf is %.2lf square units.\n", radius, area);

    return 0;
}

Here’s a step-by-step explanation of the program:

  1. We include the stdio.h header for input and output functions.
  2. We declare two variables: radius (to store the radius of the circle) and area (to store the calculated area).
  3. We use printf to ask the user to enter the radius of the circle and scanf to read the input and store it in the radius variable.
  4. We calculate the area of the circle using the formula π * r * r, where π is approximately 3.14159265359.
  5. Finally, we use printf to display the result, including the radius and the calculated area, with two decimal places.

To run this program, follow these steps:

  1. Open a C compiler (like GCC) on your computer.
  2. Copy and paste the program into a new C file (e.g., circle_area.c).
  3. Compile the program by running gcc circle_area.c -o circle_area in the command prompt or terminal.
  4. Run the program by typing ./circle_area and providing the radius when prompted.

The program will then calculate and display the area of the circle based on the radius you enter.

Leave a Reply

Your email address will not be published. Required fields are marked *