C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

C Program: Menu-Driven Geometry Calculations

Introduction

Geometry problems are widely used in real life — in engineering, construction, architecture, and graphics.
A menu-driven program makes it easy to calculate different shapes’ properties within a single application.

We’ll cover:

  1. Rectangle → Area & Perimeter
  2. Circle → Area & Circumference
  3. Square → Area & Perimeter
  4. Triangle → Area (using Heron’s Formula) & Perimeter
  5. Trapezium → Area

 

C Program: Menu-Driven Geometry Calculations

C

#include <stdio.h>

#include <math.h>    // For sqrt in triangle area

#define PI 3.14159   // Constant value of PI

 

int main() {

    int choice;

    float length, width, radius, side, base1, base2, height;

    float a, b, c;   // Sides of triangle

    float area, perimeter, circumference, s;

 

    // Display Menu

    printf("=== Geometry Calculator ===\n");

    printf("1. Rectangle (Area & Perimeter)\n");

    printf("2. Circle (Area & Circumference)\n");

    printf("3. Square (Area & Perimeter)\n");

    printf("4. Triangle (Area & Perimeter)\n");

    printf("5. Trapezium (Area)\n");

    printf("Enter your choice (1-5): ");

    scanf("%d", &choice);

 

    switch(choice) {

        case 1: // Rectangle

            printf("\nEnter length of rectangle: ");

            scanf("%f", &length);

            printf("Enter width of rectangle: ");

            scanf("%f", &width);

 

            area = length * width;

            perimeter = 2 * (length + width);

 

            printf("\nArea of Rectangle      = %.2f\n", area);

            printf("Perimeter of Rectangle = %.2f\n", perimeter);

            break;

 

        case 2: // Circle

            printf("\nEnter radius of circle: ");

            scanf("%f", &radius);

 

            area = PI * radius * radius;

            circumference = 2 * PI * radius;

 

            printf("\nArea of Circle          = %.2f\n", area);

            printf("Circumference of Circle = %.2f\n", circumference);

            break;

 

        case 3: // Square

            printf("\nEnter side of square: ");

            scanf("%f", &side);

 

            area = side * side;

            perimeter = 4 * side;

 

            printf("\nArea of Square      = %.2f\n", area);

            printf("Perimeter of Square = %.2f\n", perimeter);

            break;

 

        case 4: // Triangle

            printf("\nEnter sides of triangle (a, b, c): ");

            scanf("%f %f %f", &a, &b, &c);

 

            perimeter = a + b + c;

            s = perimeter / 2; // Semi-perimeter

            area = sqrt(s * (s - a) * (s - b) * (s - c)); // Heron's Formula

 

            printf("\nPerimeter of Triangle = %.2f\n", perimeter);

            printf("Area of Triangle      = %.2f\n", area);

            break;

 

        case 5: // Trapezium

            printf("\nEnter base1 of trapezium: ");

            scanf("%f", &base1);

            printf("Enter base2 of trapezium: ");

            scanf("%f", &base2);

            printf("Enter height of trapezium: ");

            scanf("%f", &height);

 

            area = 0.5 * (base1 + base2) * height;

 

            printf("\nArea of Trapezium = %.2f\n", area);

            break;

 

        default:

            printf("\nInvalid choice! Please select 1–5.\n");

    }

 

    return 0;

}

Output

 
OUTPUT 1 :

Example 1 (Rectangle)

=== Geometry Calculator === 1. Rectangle (Area & Perimeter) 2. Circle (Area & Circumference) 3. Square (Area & Perimeter) 4. Triangle (Area & Perimeter) 5. Trapezium (Area) Enter your choice (1-5): 1 Enter length of rectangle: 10 Enter width of rectangle: 6 Area of Rectangle = 60.00 Perimeter of Rectangle = 32.00 OUTPUT 2 :

Example 2 (Circle)

=== Geometry Calculator === 1. Rectangle (Area & Perimeter) 2. Circle (Area & Circumference) 3. Square (Area & Perimeter) 4. Triangle (Area & Perimeter) 5. Trapezium (Area) Enter your choice (1-5): 2 Enter radius of circle: 7 Area of Circle = 153.94 Circumference of Circle = 43.98 OUTPUT 3 :

Example 3 (Triangle)

=== Geometry Calculator === 1. Rectangle (Area & Perimeter) 2. Circle (Area & Circumference) 3. Square (Area & Perimeter) 4. Triangle (Area & Perimeter) 5. Trapezium (Area) Enter your choice (1-5): 4 Enter sides of triangle (a, b, c): 5 6 7 Perimeter of Triangle = 18.00 Area of Triangle = 14.70

Explanation

  1. Menu display
    • Shows options for Rectangle or Circle.
  2. User input
    • User selects 1 or 2.
  3. Switch-case
    • case 1 → Rectangle: calculates area & perimeter.
    • case 2 → Circle: calculates area & circumference.
    • default → Handles invalid choice.
  4. Formulas applied (as explained in the earlier programs).