1. Rectangle Area The area of a rectangle is calculated according to the following formula: Area=Width×LengthArea=Width×Length Design a function that accepts a rectangle’s width and length as arguments and returns the rectangle’s area. Use the function in a program that prompts the user to enter the rectangle’s width and length, and then displays the rectangle’s area.

Respuesta :

Answer:

The method is given below.

double area(double w, double l)

{

   double area = w*l;

   return area;

}

Explanation:

The cpp program using the above method is given below.

#include <iostream>

using namespace std;

double area(double w, double l)

{

   double area = w*l;

   

   return area;

}

int main() {

   

   // variables to hold parameters of the rectangle

   double width;

   double length;

   

   // user asked to enter parameters of the rectangle

   cout<<"Enter the width of the rectangle: ";

   cin>>width;

   

   cout<<"Enter the length of the rectangle: ";

   cin>>length;

   

   // area of the rectangle returned by area() and displayed

   cout<<"The area of the rectangle is "<<area(width, length)<<endl;

   

   return 0;

}

OUTPUT

Enter the width of the rectangle: 12

Enter the length of the rectangle: 8.9

The area of the rectangle is 106.8

The program includes the following.

1. The area() method takes two double parameters as argument of the rectangle.

2. The area() method computes the area using the arguments and returns this value.

3. The area() method has return type of double.

4. The area() method has a double variable, area, which holds the product of the arguments.

5. The main() method has return type of integer and returns an integer value. This is done to assure successful execution of the program.

6. Inside main(), two double variables are declared.

7. User is prompted to enter the parameters of the rectangle which are stored in these variables.

8. The user input is passed to the area() method as parameters.

9. This method returns the computed value of the area of the rectangle.

10. The area is displayed to the console. A new line is inserted at the end of the output.

11. All the variables are declared with double datatype to accommodate both decimal and non-decimal values.

12. All the code is written inside the respective method. No classes are used.

RELAXING NOICE
Relax