C++ Online Compiler
Example: Geometry Calculator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Geometry Calculator #include <iostream> #include <cmath> // For M_PI and pow function // Function to calculate area of a circle double calculateCircleArea(double radius) { return M_PI * pow(radius, 2); } // Function to calculate circumference of a circle double calculateCircleCircumference(double radius) { return 2 * M_PI * radius; } // Function to calculate area of a rectangle double calculateRectangleArea(double length, double width) { return length * width; } // Function to calculate perimeter of a rectangle double calculateRectanglePerimeter(double length, double width) { return 2 * (length + width); } // Function to calculate area of a triangle (base * height / 2) double calculateTriangleArea(double base, double height) { return 0.5 * base * height; } // Function to calculate area of a square double calculateSquareArea(double side) { return side * side; } // Function to calculate perimeter of a square double calculateSquarePerimeter(double side) { return 4 * side; } int main() { int choice; double value1, value2; // Step 1: Display the main menu do { std::cout << "\n--- Geometry Calculator ---" << std::endl; std::cout << "1. Calculate Area of Circle" << std::endl; std::cout << "2. Calculate Circumference of Circle" << std::endl; std::cout << "3. Calculate Area of Rectangle" << std::endl; std::cout << "4. Calculate Perimeter of Rectangle" << std::endl; std::cout << "5. Calculate Area of Triangle" << std::endl; std::cout << "6. Calculate Area of Square" << std::endl; std::cout << "7. Calculate Perimeter of Square" << std::endl; std::cout << "0. Exit" << std::endl; std::cout << "Enter your choice: "; std::cin >> choice; // Step 2: Process user's choice using a switch statement switch (choice) { case 1: // Area of Circle std::cout << "Enter the radius of the circle: "; std::cin >> value1; if (value1 < 0) { std::cout << "Radius cannot be negative." << std::endl; } else { std::cout << "The area of the circle is: " << calculateCircleArea(value1) << std::endl; } break; case 2: // Circumference of Circle std::cout << "Enter the radius of the circle: "; std::cin >> value1; if (value1 < 0) { std::cout << "Radius cannot be negative." << std::endl; } else { std::cout << "The circumference of the circle is: " << calculateCircleCircumference(value1) << std::endl; } break; case 3: // Area of Rectangle std::cout << "Enter the length of the rectangle: "; std::cin >> value1; std::cout << "Enter the width of the rectangle: "; std::cin >> value2; if (value1 < 0 || value2 < 0) { std::cout << "Length and width cannot be negative." << std::endl; } else { std::cout << "The area of the rectangle is: " << calculateRectangleArea(value1, value2) << std::endl; } break; case 4: // Perimeter of Rectangle std::cout << "Enter the length of the rectangle: "; std::cin >> value1; std::cout << "Enter the width of the rectangle: "; std::cin >> value2; if (value1 < 0 || value2 < 0) { std::cout << "Length and width cannot be negative." << std::endl; } else { std::cout << "The perimeter of the rectangle is: " << calculateRectanglePerimeter(value1, value2) << std::endl; } break; case 5: // Area of Triangle std::cout << "Enter the base of the triangle: "; std::cin >> value1; std::cout << "Enter the height of the triangle: "; std::cin >> value2; if (value1 < 0 || value2 < 0) { std::cout << "Base and height cannot be negative." << std::endl; } else { std::cout << "The area of the triangle is: " << calculateTriangleArea(value1, value2) << std::endl; } break; case 6: // Area of Square std::cout << "Enter the side length of the square: "; std::cin >> value1; if (value1 < 0) { std::cout << "Side length cannot be negative." << std::endl; } else { std::cout << "The area of the square is: " << calculateSquareArea(value1) << std::endl; } break; case 7: // Perimeter of Square std::cout << "Enter the side length of the square: "; std::cin >> value1; if (value1 < 0) { std::cout << "Side length cannot be negative." << std::endl; } else { std::cout << "The perimeter of the square is: " << calculateSquarePerimeter(value1) << std::endl; } break; case 0: std::cout << "Exiting Geometry Calculator. Goodbye!" << std::endl; break; default: std::cout << "Invalid choice. Please try again." << std::endl; } } while (choice != 0); // Step 3: Loop until user chooses to exit return 0; }
Output
Clear
ADVERTISEMENTS