In this program, we will learn how to find the area of a rectangle using C++ Programming language.
As we know, the area of a rectangle is the product of its length and its breadth.
- Area = Length x Breadth
So, without further ado, let’s begin this tutorial.
C++ Program to Find Area of Rectangle
// C++ Program to Find Area of Rectangle #include <iostream> using namespace std; int main(){ int length, breadth, area; // Asking for input cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the breadth of the rectangle: "; cin >> breadth; // Calculating area area = length * breadth; // Display output cout << "Area of Rectangle: " << area; return 0; }
Output
Enter the length of the rectangle: 5
Enter the breadth of the rectangle: 7
Area of Rectangle: 35
How Does This Program Work ?
int length, breadth, area;
In this program, we have declared some int data type variables named length, breadth and area.
// Asking for input cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the breadth of the rectangle: "; cin >> breadth;
Then, the user is asked to enter the length and breadth of the rectangle.
// Calculating area area = length * breadth;
We calculate area of the rectangle using the formula,
- Area = Length x Breadth
// Display output cout << "Area of Rectangle: " << area;
Finally, the output is displayed on the screen using the cout statement.
C++ Program to Find Area of Rectangle Using Functions
// C++ Program to Find Area of Rectangle Using Functions #include <iostream> using namespace std; int area_rect(int length, int breadth); int main(){ int length, breadth, area; // Asking for input cout << "Enter the length: "; cin >> length; cout << "Enter the breadth: "; cin >> breadth; // Calling out the function area = area_rect(length, breadth); // Displaying output cout << "Area of the rectangle: " << area; return 0; } int area_rect(int length, int breadth){ return (length * breadth); }
Output
Enter the length: 4
Enter the breadth: 6
Area of the rectangle: 24
int area_rect(int length, int breadth){ return (length * breadth); }
In this program, we have declared a custom function named area_rect which returns the area of the rectangle.
Conclusion
I hope after going through this post, you understand how to find the area of a rectangle using C++ Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.
Also Read:
Particularly the sorting of this programming is massive in high through the understanding, thanks for a good teacher and indeed for the program