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 dimensions allows for non-integer values. double length, width, area, perimeter; // Step 2: Prompt the user to enter the length of the rectangle. std::cout << "Enter the length of the rectangle: "; // Step 3: Read the length entered by the user. std::cin >> length; // Step 4: Prompt the user to enter the width of the rectangle. std::cout << "Enter the width of the rectangle: "; // Step 5: Read the width entered by the user. std::cin >> width; // Step 6: Calculate the area of the rectangle. // Formula: Area = length * width area = length * width; // Step 7: Calculate the perimeter of the rectangle. // Formula: Perimeter = 2 * (length + width) perimeter = 2 * (length + width); // Step 8: Display the calculated area to the user. std::cout << "The Area of the rectangle is: " << area << std::endl; // Step 9: Display the calculated perimeter to the user. std::cout << "The Perimeter of the rectangle is: " << perimeter << std::endl; return 0; // Indicate successful program execution }
Output
Clear
ADVERTISEMENTS