VOOZH about

URL: https://www.geeksforgeeks.org/computer-science-fundamentals/program-to-find-the-area-of-different-shapes-menu-driven/

⇱ Program to find the area of different shapes | Menu Driven - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find the area of different shapes | Menu Driven

Last Updated : 23 Feb, 2024

Write a Menu-driven program to calculate the area of different shapes based on the user choice.

Menu to calculate area of different shapes:

Enter 1 to calculate the area of a circle.
Enter 2 to calculate the area of a triangle.
Enter 3 to calculate the area of a square.
Enter 4 to calculate the area of a rectangle.
Enter 5 to terminate the program.

Formulae for area of different shapes:

Area of a circle = 3.14159265358979323846 * radius * radius
Area of a triangle = (1/2) * base * height
Area of a square = side * side
Area of a rectangle = length * breadth

Approach: To solve the problem, follow the below idea:

The approach is to use Switch-case to make different cases for different choices. The switch case should run until the user enters 5 to terminate the program. Use break statement to avoid fall-through between cases.

Below is the implementation of the approach:

Output:

Enter 1 to calculate the area of a circle
Enter 2 to calculate the area of a triangle
Enter 3 to calculate the area of a square
Enter 4 to calculate the area of a rectangle
Enter 5 to terminate the program
1
Enter radius: 14
Area = 615.752
Enter 1 to calculate the area of a circle
Enter 2 to calculate the area of a triangle
Enter 3 to calculate the area of a square
Enter 4 to calculate the area of a rectangle
Enter 5 to terminate the program
2
Enter base: 10
Enter height: 20
Area = 100
Enter 1 to calculate the area of a circle
Enter 2 to calculate the area of a triangle
Enter 3 to calculate the area of a square
Enter 4 to calculate the area of a rectangle
Enter 5 to terminate the program
3
Enter side length: 10
Area = 100
Enter 1 to calculate the area of a circle
Enter 2 to calculate the area of a triangle
Enter 3 to calculate the area of a square
Enter 4 to calculate the area of a rectangle
Enter 5 to terminate the program
4
Enter length: 5
Enter breadth: 10
Area = 50
Enter 1 to calculate the area of a circle
Enter 2 to calculate the area of a triangle
Enter 3 to calculate the area of a square
Enter 4 to calculate the area of a rectangle
Enter 5 to terminate the program
9
Invalid Choice
Enter 1 to calculate the area of a circle
Enter 2 to calculate the area of a triangle
Enter 3 to calculate the area of a square
Enter 4 to calculate the area of a rectangle
Enter 5 to terminate the program
5
Program terminated

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment
Article Tags: