Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C program to print area of triangle, square, circle, rectangle and polygon using switch case.
This C program calculates the area of different geometric shapes using a switch case statement. The program provides a menu−driven interface where users can select from triangle, square, circle, rectangle, and parallelogram area calculations.
Syntax
switch (expression) {
case constant1:
// statements
break;
case constant2:
// statements
break;
default:
// default statements
break;
}
Area Formulas
The program uses the following formulas for area calculations −
-
Triangle: Using Heron's formula −
area = sqrt(s*(s-a)*(s-b)*(s-c))wheres = (a+b+c)/2 -
Square:
area = side * side -
Circle:
area = ? * radius * radius -
Rectangle:
area = length * breadth -
Parallelogram:
area = base * height
Example
Following is the C program to calculate areas using switch case −
#include <stdio.h>
#include <math.h>
int main() {
int choice;
printf("Enter<br>1 to find area of Triangle<br>2 for finding area of Square<br>3 for finding area of Circle<br>4 for finding area of Rectangle<br>5 for Parallelogram<br>");
scanf("%d", &choice);
switch (choice) {
case 1: {
int a, b, c;
float s, area;
printf("Enter sides of triangle<br>");
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 %.2f<br>", area);
break;
}
case 2: {
float side, area;
printf("Enter Sides of Square<br>");
scanf("%f", &side);
area = side * side;
printf("Area of Square is %.2f<br>", area);
break;
}
case 3: {
float radius, area;
printf("Enter Radius of Circle<br>");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("Area of Circle is %.2f<br>", area);
break;
}
case 4: {
float len, breadth, area;
printf("Enter Length and Breadth of Rectangle<br>");
scanf("%f %f", &len, &breadth);
area = len * breadth;
printf("Area of Rectangle is %.2f<br>", area);
break;
}
case 5: {
float base, height, area;
printf("Enter base and height of Parallelogram<br>");
scanf("%f %f", &base, &height);
area = base * height;
printf("Area of Parallelogram is %.2f<br>", area);
break;
}
default: {
printf("Invalid Choice<br>");
break;
}
}
return 0;
}
Output
When the above program is executed, it produces the following output −
Run 1: 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 5 Enter base and height of Parallelogram 2 4 Area of Parallelogram is 8.00 Run 2: 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 3 Enter Radius of Circle 4.5 Area of Circle is 63.62
Key Points
- Each case handles a specific geometric shape with its corresponding formula.
- The
breakstatement prevents fall−through to subsequent cases. - The
defaultcase handles invalid user input. - Heron's formula is used for triangle area calculation when three sides are given.
Conclusion
This program demonstrates the effective use of switch case statements for menu−driven programs. It provides a clean way to handle multiple area calculations based on user choice while maintaining code readability.
Advertisements
