Program to find the area of different shapes | Menu Driven
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:
C++
#include <iostream> using namespace std; // function to return area of a circle double getCircleArea( double radius) { return (3.14159265358979323846 * radius * radius); } // function to return area of a triangle double getTriangleArea( double base, double height) { return (base * height * 0.5); } // function to return area of a square double getSquareArea( double side) { return side * side; } // function to return area of a rectangle double getRectangleArea( double length, double breadth) { return length * breadth; } int main() { bool flag = true ; while (flag) { cout << "Enter 1 to calculate the area of a circle" << endl; cout << "Enter 2 to calculate the area of a triangle" << endl; cout << "Enter 3 to calculate the area of a square" << endl; cout << "Enter 4 to calculate the area of a " "rectangle" << endl; cout << "Enter 5 to terminate the program" << endl; int choice; cin >> choice; switch (choice) { case 1: cout << "Enter radius: "; double radius; cin >> radius; cout << "Area = " << getCircleArea(radius) << endl; break ; case 2: cout << "Enter base: "; double base, height; cin >> base; cout << "Enter height: "; cin >> height; cout << "Area = " << getTriangleArea(base, height) << endl; break ; case 3: cout << "Enter side length: "; double side; cin >> side; cout << "Area = " << getSquareArea(side) << endl; break ; case 4: cout << "Enter length: "; double length, breadth; cin >> length; cout << "Enter breadth: "; cin >> breadth; cout << "Area = " << getRectangleArea(length, breadth) << endl; break ; case 5: flag = false ; break ; default : cout << "Invalid Choice" << endl; } } cout << "Program terminated" << endl; return 0; } |
Java
import java.util.Scanner; public class AreaCalculator { // function to return area of a circle static double getCircleArea( double radius) { return Math.PI * radius * radius; } // function to return area of a triangle static double getTriangleArea( double base, double height) { return base * height * 0.5 ; } // function to return area of a square static double getSquareArea( double side) { return side * side; } // function to return area of a rectangle static double getRectangleArea( double length, double breadth) { return length * breadth; } public static void main(String[] args) { boolean flag = true ; Scanner scanner = new Scanner(System.in); while (flag) { try { System.out.println( "Enter 1 to calculate the area of a circle" ); System.out.println( "Enter 2 to calculate the area of a triangle" ); System.out.println( "Enter 3 to calculate the area of a square" ); System.out.println( "Enter 4 to calculate the area of a rectangle" ); System.out.println( "Enter 5 to terminate the program" ); int choice = scanner.nextInt(); switch (choice) { case 1 : System.out.print( "Enter radius: " ); double radius = scanner.nextDouble(); System.out.println( "Area = " + getCircleArea(radius)); break ; case 2 : System.out.print( "Enter base: " ); double base = scanner.nextDouble(); System.out.print( "Enter height: " ); double height = scanner.nextDouble(); System.out.println( "Area = " + getTriangleArea(base, height)); break ; case 3 : System.out.print( "Enter side length: " ); double side = scanner.nextDouble(); System.out.println( "Area = " + getSquareArea(side)); break ; case 4 : System.out.print( "Enter length: " ); double length = scanner.nextDouble(); System.out.print( "Enter breadth: " ); double breadth = scanner.nextDouble(); System.out.println( "Area = " + getRectangleArea(length, breadth)); break ; case 5 : flag = false ; break ; default : System.out.println( "Invalid Choice" ); } } catch (java.util.NoSuchElementException e) { System.out.println( "No input provided. Exiting..." ); break ; } } System.out.println( "Program terminated" ); scanner.close(); } } |
Python3
import math # Function to return area of a circle def get_circle_area(radius): return math.pi * radius * * 2 # Function to return area of a triangle def get_triangle_area(base, height): return 0.5 * base * height # Function to return area of a square def get_square_area(side): return side * * 2 # Function to return area of a rectangle def get_rectangle_area(length, breadth): return length * breadth # Main program flag = True while flag: print ( "Enter 1 to calculate the area of a circle" ) print ( "Enter 2 to calculate the area of a triangle" ) print ( "Enter 3 to calculate the area of a square" ) print ( "Enter 4 to calculate the area of a rectangle" ) print ( "Enter 5 to terminate the program" ) choice = int ( input ( "Enter your choice: " )) if choice = = 1 : radius = float ( input ( "Enter radius: " )) print ( "Area =" , get_circle_area(radius)) elif choice = = 2 : base = float ( input ( "Enter base: " )) height = float ( input ( "Enter height: " )) print ( "Area =" , get_triangle_area(base, height)) elif choice = = 3 : side = float ( input ( "Enter side length: " )) print ( "Area =" , get_square_area(side)) elif choice = = 4 : length = float ( input ( "Enter length: " )) breadth = float ( input ( "Enter breadth: " )) print ( "Area =" , get_rectangle_area(length, breadth)) elif choice = = 5 : flag = False else : print ( "Invalid Choice" ) print ( "Program terminated" ) |
C#
using System; public class Scanner : IDisposable { private readonly System.IO.TextReader _reader; private string _inputBuffer = string .Empty; public Scanner(System.IO.TextReader reader = null ) { _reader = reader ?? Console.In; } public void Dispose() { _reader.Dispose(); } public int NextInt() { return int .Parse(Next()); } public double NextDouble() { return double .Parse(Next()); } private string Next() { if ( string .IsNullOrEmpty(_inputBuffer)) { _inputBuffer = _reader.ReadLine(); if (_inputBuffer == null ) { throw new InvalidOperationException( "Not enough input." ); } } var tokens = _inputBuffer.Split( new [] { ' ' , '\t' }, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 0) { throw new InvalidOperationException( "Not enough input." ); } _inputBuffer = string .Empty; return tokens[0]; } public void ClearBuffer() { _inputBuffer = string .Empty; } } public class AreaCalculator { // function to return area of a circle static double GetCircleArea( double radius) { return Math.PI * radius * radius; } // function to return area of a triangle static double GetTriangleArea( double baseLength, double height) { return baseLength * height * 0.5; } // function to return area of a square static double GetSquareArea( double side) { return side * side; } // function to return area of a rectangle static double GetRectangleArea( double length, double breadth) { return length * breadth; } public static void Main( string [] args) { bool flag = true ; Scanner scanner = new Scanner(); while (flag) { try { Console.WriteLine( "Enter 1 to calculate the area of a circle" ); Console.WriteLine( "Enter 2 to calculate the area of a triangle" ); Console.WriteLine( "Enter 3 to calculate the area of a square" ); Console.WriteLine( "Enter 4 to calculate the area of a rectangle" ); Console.WriteLine( "Enter 5 to terminate the program" ); int choice = scanner.NextInt(); switch (choice) { case 1: Console.Write( "Enter radius: " ); double radius = scanner.NextDouble(); Console.WriteLine( "Area = " + GetCircleArea(radius)); break ; case 2: Console.Write( "Enter base: " ); double baseLength = scanner.NextDouble(); Console.Write( "Enter height: " ); double height = scanner.NextDouble(); Console.WriteLine( "Area = " + GetTriangleArea(baseLength, height)); break ; case 3: Console.Write( "Enter side length: " ); double side = scanner.NextDouble(); Console.WriteLine( "Area = " + GetSquareArea(side)); break ; case 4: Console.Write( "Enter length: " ); double length = scanner.NextDouble(); Console.Write( "Enter breadth: " ); double breadth = scanner.NextDouble(); Console.WriteLine( "Area = " + GetRectangleArea(length, breadth)); break ; case 5: flag = false ; break ; default : Console.WriteLine( "Invalid Choice" ); break ; } } catch (InvalidOperationException e) { Console.WriteLine(e.Message); break ; } } Console.WriteLine( "Program terminated" ); } } |
Javascript
// Function to return area of a circle function getCircleArea(radius) { return Math.PI * Math.pow(radius, 2); } // Function to return area of a triangle function getTriangleArea(base, height) { return 0.5 * base * height; } // Function to return area of a square function getSquareArea(side) { return Math.pow(side, 2); } // Function to return area of a rectangle function getRectangleArea(length, breadth) { return length * breadth; } // Main program let flag = true ; while (flag) { console.log( "Enter 1 to calculate the area of a circle" ); console.log( "Enter 2 to calculate the area of a triangle" ); console.log( "Enter 3 to calculate the area of a square" ); console.log( "Enter 4 to calculate the area of a rectangle" ); console.log( "Enter 5 to terminate the program" ); let choice = prompt( "Enter your choice: " ); if (choice == 1) { let radius = parseFloat(prompt( "Enter radius: " )); console.log( "Area =" , getCircleArea(radius)); } else if (choice == 2) { let base = parseFloat(prompt( "Enter base: " )); let height = parseFloat(prompt( "Enter height: " )); console.log( "Area =" , getTriangleArea(base, height)); } else if (choice == 3) { let side = parseFloat(prompt( "Enter side length: " )); console.log( "Area =" , getSquareArea(side)); } else if (choice == 4) { let length = parseFloat(prompt( "Enter length: " )); let breadth = parseFloat(prompt( "Enter breadth: " )); console.log( "Area =" , getRectangleArea(length, breadth)); } else if (choice == 5) { flag = false ; } else { console.log( "Invalid Choice" ); } } console.log( "Program terminated" ); |
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)
Contact Us