Programming Challenge Chapter 4 – Q #4 Areas of Rectangles – Tony Gaddis – Starting Out With C++
Problem: -
Areas of Rectangles
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 ()
{
//Declare Variables For Input
int length1, width1, area1;
int length2, width2, area2;
//Take Input one by one
cout<<"Enter length of Rectangle A : ";
cin>>length1;
cout<<"Enter width of Rectangle A : ";
cin>>width1;
cout<<"Enter length of Rectangle B : ";
cin>>length2;
cout<<"Enter width of Rectangle B : ";
cin>>width2;
//Calculate Areas
area1 = length1*width1;
area2 = length2*width2;
//Use Condition For Checking
if (area1>area2)
cout<<"The Area of Rectangle A is Greater than Rectangle B";
else
cout<<"The Area of Rectangle B is Greater than Rectangle A";
//Return 0 To Main Function
return 0;
}
This is the solution of this question
OUTPUT OF THIS QUESTION
Explanation of this Solution
Solution Explain With Comments in Solution.
Also, I attach CPP file of this problem. You can download this file
0 Comments