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...
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:
- Declare
intvariables namedlengthandwidthand assign them suitable values. - Declare an
intvariable calledareaand compute it aslength * width. - Output the value of
areausingcout.
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.