Friday, November 18, 2016

Programming Challenge 4.4 - Twice Rectangular

/* Twice Rectangular - This program asks for the length and width of two rectangles.
   The program tells the user which rectangle has the greater area, or if the areas
   are the same.
  
   The formula to calculate the area of a rectangle is length * width. */

#include "Utility.h"

int main()
{
    /* Hold length, width, areaRect1 and areaRect2 */
    int lengthRect1,
        widthRect1,
        lengthRect2,
        widthRect2,
        areaRect1,
        areaRect2;

    /* Ask the user for input of length and width */
    cout << "Enter length of rectangle 1 and 2: ";
    cin >> lengthRect1 >> lengthRect2;
    cout << "Enter width of rectangle 1 and 2: ";
    cin >> widthRect1 >> widthRect2;

    /* Calculate the area for both rectangles */
    areaRect1 = lengthRect1 * widthRect1;
    areaRect2 = lengthRect2 * widthRect2;

    /* Determine which rectangle has the greater area */
    if (areaRect1 > areaRect2)
    {
        cout << "Rectangle 1 has the greater area.\n";
    }
    else if (areaRect1 < areaRect2)
    {
        cout << "Rectangle 2 has the greater area.\n";
    }
    else
    {
        cout << "Both rectangles have the same area.\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment