Skip to main content
Back to Blog
Programming Languages
14 August 20263 min readUpdated 14 August 2026

Understanding C++ Variables: Calculating Rectangle Area

C++ Variables Explained: Calculate Rectangle Area Explore the concept of C++ variables through a practical example of calculating a rectangle's area. Instructions: In the functi...

Understanding C++ Variables: Calculating Rectangle Area

C++ Variables Explained: Calculate Rectangle Area

Explore the concept of C++ variables through a practical example of calculating a rectangle's area.

Instructions:

In the main() function, follow these steps to complete the task:

  1. Declare int variables named length and width and assign them suitable values.
  2. Declare an int variable called area and compute it as length * width.
  3. Output the value of area using cout.

Ensure each statement ends with a semicolon.

#include <iostream>
using namespace std;

int main() {
    // Declare and initialize length
    int length = 4;
  
    // Declare and initialize width
    int width = 6;
  
    // Calculate area
    int area = length * width;
  
    // Print the calculated area
    cout << area;
    
    return 0;
}

This simple example demonstrates how variables work in C++ and how they can be used in calculations.