C++ Program to Calculate Area of Rectangle

Howdy readers, today you will learn how to write a C++ program to calculate the area of a rectangle.

The area of a rectangle (A) is the product of its length l and width b. So, the area of the rectangle is:  Area of Rectangle (A) = Length (a) x Width (b).

This program uses the above formula to find the area of a rectangle.

So, without any delay, let’s begin this tutorial.

C++ Program to Calculate Area of Rectangle

C++ Program

// C++ Program to Calculate Area of Rectangle
#include <iostream>
using namespace std;

int main(){
    int length, breadth;
    float area;
    
    // Taking input
    cout << "Enter the length of the rectangle: ";
    cin >> length;
    cout << "Enter the breadth of the rectangle: ";
    cin >> breadth;
    
    // Calculating area of rectangle using formula
    area = length * breadth;
    
    // Display area
    cout << "The area of the rectangle is: " << area << endl;
    
    return 0;
}

Output

Enter the length of the rectangle: 12
Enter the breadth of the rectangle: 16
The area of the rectangle is: 192
C++ Program to Calculate Area of Rectangle

Explanation

    int length, breadth;
    float area;

In this program, we have declared two int data type variables and one float data type variable named length, breadth and area respectively.

    cout << "Enter the length of the rectangle: ";
    cin >> length;
    cout << "Enter the breadth of the rectangle: ";
    cin >> breadth;

The user is asked to enter the value of the length and breadth of the rectangle.

    area = length * breadth;

The area of the rectangle is obtained using the formula: A = l x b, where ‘l’ is the length and ‘b’ is the breadth of the rectangle.

    cout << "The area of the rectangle is: " << area << endl;

After that, the area of the rectangle is printed on the screen using the cout statement.

Conclusion

Today you learned how to write a C++ program to calculate the area of a rectangle.

Don’t forget to ask your queries in the comment section.

Thanks for reading.

Happy Coding!!

Also Read:

Leave a Comment