w3resource

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

C Conditional Statement: Exercise-25 with Solution

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.
}

Sample 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

C Programming Code Editor:

Previous: Write a program in C to read any Month Number in integer and display the number of days for this month.
Next: Write a program in C which is a Menu-Driven Program to perform a simple calculation.

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.