Chapter 4 – #4: Areas of Rectangles – Tony Gaddis – Starting Out With C++

>_ Problem

The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length and width of two rectangles. The program should tell the user which rectangle has the greater area, or if the areas are the same.

>_ Solution

#include <iostream>
using namespace std;

int main()
    // Variables
    float l1, 

    // Ask the user for l and w of 2 rectangles
    cout << endl;
    cout << "What is the length of the first rectangle: ";
    cin >> l1;
    cout << "What is the width: ";
    cin >> w1;
    cout << "What is the length of the second rectangle: ";
    cin >> l2;
    cout << "The width: ";
    cin >> w2;
    cout << endl;

    // Calculate area for each rectangle
    rectangle1 = l1 * w1;
    rectangle2 = l2 * w2;

    // Decide & display rectangle with greatest area
    if (rectangle1 > rectangle2)
        cout << "Area for rectangle 1 is greater." << endl;
    else if (rectangle1 < rectangle2)
        cout << "Area for rectangle 2 is greater." << endl;
    else if (rectangle1 == rectangle2)
        cout << "Area for both rectangles is the same." << endl;

    // Formatting last line.
    cout << endl;
    // Terminate program
    return 0;

>_Get The Book

Tony Gaddis’ book “Starting Out with C++ from Control Structures to Objects”: 8th and 9th editions.

>_ Video Walk Through

This video includes a step by step process of how I solve programming challenge #4.

>_That’s it!

Thank you for taking interest in what I do! I hope it was helpful for you as much as it helped me along my journey in learning to code!

>_ Also Check Out:

Tag: Starting Out With C++ | Chapter 4 | Programming Challenge Solutions

Leave a Reply