- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 print area of triangle, square, circle, rectangle and polygon using switch case.
Problem
Write a program to calculate the area of triangle, square, circle, rectangle and polygon by using the switch case.
Solution
Based on case number, the area of triangle, square, circle, rectangle and polygon is calculated.
- The logic used to find area of triangle is as follows −
Enter sides of a triangle a,b,c
s=(float)(a+b+c)/2; area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));
- The logic used to find area of square is as follows −
Enter the side of square at runtime.
area=(float)side*side;
- The logic used to find area of circle is as follows −
Enter the radius of circle at runtime
area=(float)3.14159*radius*radius;
- The logic used to find area of rectangle is as follows −
Enter length and breadth of rectangle at runtime
area=(float)len*breadth;
- The logic used to find area of parallelogram is as follows −
Enter base and height of parallelogram
area=(float)base*height;
Example
Following is the C program to calculate the area of triangle, square, circle, rectangle and polygon by using the switch case −
#include<stdio.h> #include<math.h> main(){ int choice; printf("Enter
1 to find area of Triangle
2 for finding area of Square
3 for finding area of Circle
4 for finding area of Rectangle
5 for Parallelogram
"); scanf("%d",&choice); switch(choice) { case 1: { int a,b,c; float s,area; printf("Enter sides of triangle
"); scanf("%d%d %d",&a,&b,&c); s=(float)(a+b+c)/2; area=(float)(sqrt(s*(s-a)*(s-b)*(s-c))); printf("Area of Triangle is %f
",area); break; } case 2: { float side,area; printf("Enter Sides of Square
"); scanf("%f",&side); area=(float)side*side; printf("Area of Square is %f
",area); break; } case 3: { float radius,area; printf("Enter Radius of Circle
"); scanf("%f",&radius); area=(float)3.14159*radius*radius; printf("Area of Circle %f
",area); break; } case 4: { float len,breadth,area; printf("Enter Length and Breadth of Rectangle
"); scanf("%f %f",&len,&breadth); area=(float)len*breadth; printf("Area of Rectangle is %f
",area); break; } case 5: { float base,height,area; printf("Enter base and height of Parallelogram
"); scanf("%f %f",&base,&height); area=(float)base*height; printf("Enter area of Parallelogram is %f
",area); break; } default: { printf("Invalid Choice
"); break; } } }
Output
When the above program is executed, it produces the following output −
When the above program is executed, it produces the following output: Run 1: 1 to find area of Triangle 2 for finding area of Square 3 for finding area of Circle 4 for finding area of Rectangle 5 for Parallelogram 5 Enter base and height of Parallelogram 2 4 6 8 Enter area of Parallelogram is 8.000000 Run 2: 1 to find area of Triangle 2 for finding area of Square 3 for finding area of Circle 4 for finding area of Rectangle 5 for Parallelogram 3 Enter Radius of Circle 4.5 Area of Circle is 63.617199
Advertisements