Articles

C++ program to find the area of a rectangle

C++ program to find the area of a rectangle

In this chapter of C++ program tutorial out task is: 

  • Write a C++ program to take length and breadth of the rectangle as an user input and displaying the area of the rectangle

C++ program to calculate the area of the rectangle 

Below is the C++ program to take length and breadth as an user input and then calculating and displaying the area of the rectange.

#include<iostream>
using namespace std;
int main()
{
        float length , breadth , areaOfTheReactangle;
        cout<<"Enter the length and breadth of the rectangle separated by a space"<<endl;
        cin>>length>>breadth;
        areaOfTheReactangle = length * breadth;  //Calculating the area of the rectangle
        cout<<"The area of the rectangle is"<<endl<<areaOfTheReactangle;
        return 0;
}

OUTPUT :

Enter the length and breadth of the rectangle separated by a space

2.3 3.0

The area of the rectangle is

6.9

 

EXPLANATION:

In the above C++ program:

  • The compilation starts from the main function (here int main())
  • Then three 4 bits each memory space is created for the float data type (assuming to be 32/64 bit system)
  • Then the compiler accepts input for the "length" and "breadth"
  • Then the "areaOfTheRectangle" is calculated by the formula and the output the required value
  • The program terminates at the "return 0" statement but returning 0 in the output screen as int main requires a return type.

You can see the above code execution and output in codeblocks IDE:

C++ program to find the area of a rectangle

 


CPP Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sayantan Bose
A coder who learns from mistakes
Page Views :    Published Date : Jul 24,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!