Area calculator program in C

Here’s an example of an area calculator program in C that calculates the area of different shapes such as a rectangle, circle, and triangle:

#include <stdio.h>

float calculateRectangleArea(float length, float width) {
    return length * width;

float calculateCircleArea(float radius) {
    return 3.14159 * radius * radius;

float calculateTriangleArea(float base, float height) {
    return 0.5 * base * height;

int main() {
    int choice;
    float length, width, radius, base, height;

    // Prompt the user to select the shape
    printf("Select the shape:\n");
    printf("1. Rectangle\n");
    printf("2. Circle\n");
    printf("3. Triangle\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    // Perform the calculation based on user choice
    switch(choice) {
        case 1:
            // Rectangle
            printf("Enter length: ");
            scanf("%f", &length);

            printf("Enter width: ");
            scanf("%f", &width);

            printf("Area of the rectangle: %.2f\n", calculateRectangleArea(length, width));
        case 2:
            // Circle
            printf("Enter radius: ");
            scanf("%f", &radius);

            printf("Area of the circle: %.2f\n", calculateCircleArea(radius));
        case 3:
            // Triangle
            printf("Enter base: ");
            scanf("%f", &base);

            printf("Enter height: ");
            scanf("%f", &height);

            printf("Area of the triangle: %.2f\n", calculateTriangleArea(base, height));
            printf("Invalid choice. Please try again.\n");

    return 0;

To compile and run the program, follow these steps:

  1. Save the code in a file with a .c extension, such as area_calculator.c.
  2. Open a terminal or command prompt and navigate to the directory where the file is saved.
  3. Compile the program using a C compiler (e.g., GCC) by running the following command: gcc -o area_calculator area_calculator.c This will create an executable file named area_calculator.
  4. Run the program by executing the following command: ./area_calculator

The program prompts the user to select a shape (rectangle, circle, or triangle) and then asks for the required dimensions. Based on the user’s choice, the program performs the corresponding area calculation and displays the calculated area on the screen.

Feel free to modify and enhance the program as per your specific requirements.

Posted in: C