C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

C Program: Menu-Driven Shape Calculations

Introduction

In programming, menu-driven programs allow users to choose between different operations from a list of options.
Here, we combine rectangle and circle calculations into a single program.

  • For Rectangle:

Area = Length × Width

Perimeter = 2 × (Length + Width)

  • For Circle:

Area = π × r2

Circumference = 2 × π × r

This program demonstrates how to use switch-case for multiple choices.

 

C Program: Area and Circumference of a Circle

C

#include <stdio.h>

#define PI 3.14159   // Defining constant value of PI

 

int main() {

    int choice;

    float length, width, radius, area, perimeter, circumference;

 

    // Display Menu

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

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

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

    printf("Enter your choice (1 or 2): ");

    scanf("%d", &choice);

 

    switch(choice) {

        case 1:

            // Rectangle calculation

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

            scanf("%f", &length);

            printf("Enter the width of the 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 calculation

            printf("\nEnter the radius of the 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;

 

        default:

            printf("\nInvalid choice! Please select 1 or 2.\n");

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
=== Geometry Calculator ===
1. Area and Perimeter of Rectangle
2. Area and Circumference of Circle
Enter your choice (1 or 2): 1

Enter the length of the rectangle: 8
Enter the width of the rectangle: 5

Area of Rectangle      = 40.00
Perimeter of Rectangle = 26.00


OUTPUT 2 :
=== Geometry Calculator ===
1. Area and Perimeter of Rectangle
2. Area and Circumference of Circle
Enter your choice (1 or 2): 2

Enter the radius of the circle: 6

Area of Circle          = 113.10
Circumference of Circle = 37.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).