C++ Online Compiler
Example: Rectangle Area and Perimeter Calculator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Rectangle Area and Perimeter Calculator #include <iostream> // Required for input/output operations int main() { // Step 1: Declare variables to store length, width, area, and perimeter // Using 'double' for precision to handle decimal values double length; double width; double area; double perimeter; // Step 2: Prompt the user to enter the length std::cout << "Enter the length of the rectangle: "; std::cin >> length; // Read the length from user input // Step 3: Prompt the user to enter the width std::cout << "Enter the width of the rectangle: "; std::cin >> width; // Read the width from user input // Step 4: Calculate the area using the formula: Area = Length * Width area = length * width; // Step 5: Calculate the perimeter using the formula: Perimeter = 2 * (Length + Width) perimeter = 2 * (length + width); // Step 6: Display the calculated area std::cout << "Area of the rectangle: " << area << std::endl; // Step 7: Display the calculated perimeter std::cout << "Perimeter of the rectangle: " << perimeter << std::endl; return 0; // Indicate successful execution }
Output
Clear
ADVERTISEMENTS