- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to find the areas of geometrical figures using switch case
Problem
Find the areas of rectangle, square, triangle, circle by using the switch case statement, User need to enter base, height, side, radius, breadth and length at runtime to calculate the areas of all geometrical figures.
Solution
The solution to find the areas of rectangle, square, triangle, circle by using the switch case statement is explained below −
Formulae
The formulae for finding the areas of the respective geometrical figures are as follows −
- Area of rectangle = breadth *length;
- Area of square = side * side;
- Area of circle = 3.142*radius*radius;
- Area of triangle = 0.5 *base*height;
Example
Following is the C program to find the areas of rectangle, square, triangle, circle by using the switch case statement −
#include <stdio.h> void main(){ int fig_code; float side, base, length, breadth, height, area, radius; printf("-------------------------
"); printf(" 1 --> Circle
"); printf(" 2 --> Rectangle
"); printf(" 3 --> Triangle
"); printf(" 4 --> Square
"); printf("-------------------------
"); printf("Enter the Figure code
"); scanf("%d", &fig_code); switch(fig_code){ case 1: printf(" Enter the radius
"); scanf("%f",&radius); area=3.142*radius*radius; printf("Area of a circle=%f
", area); break; case 2: printf(" Enter the breadth and length
"); scanf("%f %f",&breadth, &length); area=breadth *length; printf("Area of a Rectangle=%f
", area); break; case 3: printf(" Enter the base and height
"); scanf("%f %f", &base, &height); area=0.5 *base*height; printf("Area of a Triangle=%f
", area); break; case 4: printf(" Enter the side
"); scanf("%f", &side); area=side * side; printf("Area of a Square=%f
", area); break; default: printf(" Error in figure code
"); break; } }
Output
When the above program is executed, it produces the following result −
Run 1: ------------------------- 1 --> Circle 2 --> Rectangle 3 --> Triangle 4 --> Square ------------------------- Enter the Figure code 3 Enter the base and height 4 7 Area of a Triangle=14.000000 Run 2: ------------------------- 1 --> Circle 2 --> Rectangle 3 --> Triangle 4 --> Square ------------------------- Enter the Figure code 1 Enter the radius 8 Area of a circle=201.087997
- Related Articles
- Write a C program of library management system using switch case
- Java program to generate a calculator using the switch case
- Find the areas of the following figures by counting square:"
- Java Program to Make a Simple Calculator Using switch...case
- Golang Program to make a Simple Calculator using Switch Case
- C program to print area of triangle, square, circle, rectangle and polygon using switch case.
- Using range in switch case in C/C++
- Java program to find whether given character is vowel or consonant using switch case
- C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case
- Converting digits to word format using switch case in C language
- Switch case statement in C
- Write a C program for electing a candidate in Elections by calling functions using Switch case
- Explain nested switch case in C language
- What is difference between using if/else and switch-case in C#?
- How to use case-insensitive switch-case in JavaScript?

Advertisements