Write a C++ program to find out the area of rectangle and display the result on the screen.
![]() |
Write a C++ program to find out the area of rectangle and display the result on the screen. |
C++ is a powerful programming language used for developing a wide range of software applications. One of the most basic concepts in geometry is calculating the area of a rectangle. In this article, we will walk through a C++ program that calculates the area of a rectangle and displays the result on the screen.
Before we dive into the program, let’s briefly discuss the formula for calculating the area of a rectangle. The area of a rectangle is equal to the product of its length and width. In mathematical terms, we can represent this formula as:
Area = length * width
Now let's move on to the program. The program consists of two parts: input and output. In the input part, we will ask the user to enter the length and width of the rectangle. In the output part, we will calculate the area of the rectangle and display it on the screen.
Here is the complete program:
#include <iostream>
using namespace std;
int main()
{
float length, width, area;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
area = length * width;
cout << "The area of the rectangle is: " << area << endl;
return 0;
}
Let’s walk through the code line by line:
1:- #include <iostream> - This line includes the input/output stream library, which allows us to perform input and output operations on the console.
2:- using namespace std; - This line declares that we will be using the standard namespace. This namespace includes the standard C++ library, which provides a set of functions and data types that are commonly used in C++ programs.
3:- int main() - This line declares the main function of our program. The main function is the entry point of our program, and it is where our program starts executing.
4:- float length, width, area; - This line declares three variables of the float data type: length, width, and area. The float data type is used to store decimal values.
5:- cout << "Enter the length of the rectangle: "; - This line uses the cout object to display a message on the console, prompting the user to enter the length of the rectangle
6:- cin >> length; - This line uses the cin object to read the value entered by the user and store it in the length variable.
7:- cout << "Enter the width of the rectangle: "; - This line uses the cout object to display a message on the console, prompting the user to enter the width of the rectangle.
8:- cin >> width; - This line uses the cin object to read the value entered by the user and store it in the width variable.
9:- area = length * width; - This line calculates the area of the rectangle using the formula we discussed earlier.
10:- cout << "The area of the rectangle is: " << area << endl; - This line uses the cout object to display the calculated area of the rectangle on the console.
11:- return 0; - This line signals to the operating system that our program has completed successfully.
Now that we have gone through the code, let’s run the program and see what happens.
When we run the program, we are prompted to enter the length and width of the rectangle. Once we have entered these values, the program calculates the area of the rectangle and displays it on the console. Here is an example output:
Enter the length of the rectangle: 5.5
Enter the width of the rectangle: 3.5
The area of the rectangle is:
0 Comments