This C++ program computes area of a shape using switch case. The program takes shape as an input and identifies the shape using a switch case. The statements then takes required values as input, computes the area and prints it.
Here is the source code of the C++ program computes area of a shape using switch case. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Find Area of Shapes using Switch Case
*/
#include <iostream>
const float PI = 3.14;
void calculateArea(std::string shape)
{
/* Comparing first character of shape string */
switch (shape[0]) {
case 'c':
float r;
std::cout << "\nEnter the radius ";
std::cin >> r;
std::cout << "\nArea of Circle : " << PI * r * r
<< " square units" << std::endl;
break;
case 's':
float a;
std::cout << "\nEnter the side length ";
std::cin >> a;
std::cout << "\nArea of Square : " << a * a
<< " square units" << std::endl;
break;
case 'r':
float b, h;
std::cout << "\nEnter the breadth ";
std::cin >> b;
std::cout << "\nEnter the height ";
std::cin >> h;
std::cout << "\nArea of Rectangle : " << b * h
<< " square units" << std::endl;
break;
}
}
int main()
{
std::string s;
std::cout << "Enter the shape : ";
getline(std::cin, s);
calculateArea(s);
}
$ a.out Enter the shape : rectangle Enter the breadth 20 Enter the height 40 Area of Rectangle : 800 square units C:\MinGW\bin>a.exe Enter the shape : circle Enter the radius 10 Area of Circle : 314 square units
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Programming MCQs
- Check Computer Science Books
- Check Programming Books
- Check C++ Books
- Apply for Computer Science Internship