w3resource

C Exercises: A menu-driven program to compute the area of the various geometrical shape


25. Geometrical Shapes Area Calculator

Write a C program which computes the area of various geometrical shapes using a menu-driven approach.

Visual Presentation:

A menu-driven program to compute the area of the various geometrical shape


Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.

void main ()
{
      int choice,r,l,w,b,h;  // Declare variables to store user input and dimensions.
      float area;  // Declare a variable to store the calculated area.

      // Display the options to the user.
      printf("Input 1 for area of circle\n");
      printf("Input 2 for area of rectangle\n");
      printf("Input 3 for area of triangle\n");
      printf("Input your choice : ");
      scanf("%d",&choice);  // Read and store the user's choice.

      switch(choice)  // Start a switch statement based on the user's choice.
      {
           case 1:
                 printf("Input radius of the circle : ");  // Prompt user for circle's radius.
                 scanf("%d",&r);  // Read and store the radius.
                 area=3.14*r*r;  // Calculate the area of the circle.
                 break;
            case 2:
                  printf("Input length and width of the rectangle : ");  // Prompt user for rectangle's dimensions.
                  scanf("%d%d",&l,&w);  // Read and store length and width.
                  area=l*w;  // Calculate the area of the rectangle.
                  break;
            case 3:
                  printf("Input the base and height of the triangle :");  // Prompt user for triangle's base and height.
                  scanf("%d%d",&b,&h);  // Read and store base and height.
                  area=.5*b*h;  // Calculate the area of the triangle.
                  break;
          }

          printf("The area is : %f\n",area);  // Display the calculated area.
}

Output:

Input 1 for area of circle                                                                                    
Input 2 for area of rectangle                                                                                 
Input 3 for area of triangle                                                                                  
Input your choice : 1                                                                                         
Input radious of the circle : 5                                                                               
The area is : 78.500000

Flowchart:

Flowchart: A menu driven program to compute the area of various geometrical shape


For more Practice: Solve these Related Problems:

  • Write a C program to compute the area of various shapes using a menu-driven approach with function pointers.
  • Write a C program to calculate the area of geometric shapes and validate user input for negative dimensions.
  • Write a C program to design a menu-driven program for calculating the area of a circle, rectangle, and triangle using structures.
  • Write a C program to implement a menu-driven area calculator that handles invalid menu choices gracefully.

Go to:


PREV : Days in a Month.
NEXT : Simple Calculation Menu.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.