Calculate the Perimeter of a Rectangle in C++

This exercise focuses on computing the perimeter of a rectangle using basic arithmetic operations in C++. It’s a great way for beginners to get hands-on practice with input/output, variables, and mathematical formulas.

In this program, the user will be prompted to enter the length and width of a rectangle. Then, the program will use the perimeter formula (Perimeter = 2 × (length + width)) to calculate and display the result.

Understanding how to apply simple mathematical formulas in code is essential for many real-world applications, such as geometry tools, construction planning software, and game development.

Group

Introduction to C++

Objective

1. Declare two floating-point variables to store the length and width of the rectangle.
2. Ask the user to input the length and width.
3. Use the formula: perimeter = 2 * (length + width) to compute the perimeter.
4. Display the perimeter result with a clear message.

Calculate the perimeter of a rectangle.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library for input and output
#include <iomanip> // Include iomanip for formatting output precision

using namespace std; // Use the standard namespace

// Main function - program entry point
int main() {
    double length, width, perimeter; // Declare variables for length, width, and perimeter

    // Prompt the user to enter the length of the rectangle
    cout << "Enter the length of the rectangle: ";
    cin >> length; // Read the length from user input

    // Prompt the user to enter the width of the rectangle
    cout << "Enter the width of the rectangle: ";
    cin >> width; // Read the width from user input

    // Calculate the perimeter using the formula: 2 * (length + width)
    perimeter = 2 * (length + width);

    // Set output precision to 2 decimal places and display the result
    cout << fixed << setprecision(2);
    cout << "The perimeter of the rectangle is: " << perimeter << endl;

    return 0; // Return 0 to indicate successful execution
}

 Output

Enter the length of the rectangle: 8
Enter the width of the rectangle: 4
The perimeter of the rectangle is: 24.00

Share this C++ Exercise


More C++ Programming Exercises of Introduction to C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Sum of the First N Natural Numbers in C++

    This exercise introduces the concept of summation using a loop in C++. You will create a program that calculates the sum of the first N natural numbers, where N is provided by the ...

  • Print the First 10 Fibonacci Numbers in C++

    This exercise helps you understand how to generate a sequence of numbers based on mathematical logic using iterative programming. Specifically, you'll write a program in C++ that p...

  • Hello World Program in C++

    The "Hello, World!" program is one of the simplest programs to write in any programming language. It serves as an introduction to the syntax and basic functionality of a programmin...

  • Sum of Two Numbers Entered by the User in C++

    This exercise introduces user input handling in C++ through a simple program that adds two numbers provided by the user. It's an ideal starting point for understanding how to work ...

  • Convert Celsius to Fahrenheit in C++

    This beginner-friendly exercise demonstrates how to perform a temperature conversion from Celsius to Fahrenheit using a basic arithmetic formula in C++. It's an excellent introduct...

  • Calculate the Area of a Circle in C++

    This exercise helps you practice using variables, mathematical operations, and user input/output in C++ by calculating the area of a circle. The formula to compute the area of a ci...