Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Areas of Rectangles. The area of a rectangle is the rectangle’s length times its

ID: 3590252 • Letter: A

Question

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. It should also tell if any of the inputs are invalid (i.e. negative). Show output tests for all three conditions.

Sum of Numbers .

Hand in your code and 2 runs (testing a positive and a negative number entered).

Write a program that asks the user for a positive value. The program should use a loop to get the sum of all the integers from 1 up to the number entered.

For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, …50.

Input validation: Do not accept a negative number.

Explanation / Answer

Sum of Numbers:

#include <iostream>

using namespace std;

int main()
{
int n;
cout<<"Enter the positive number: "<<endl;
cin >> n;
while(n < 0) {
cout<<"Invalid input. ";
cout<<"Enter the positive number: "<<endl;
cin >> n;
}
int sum = 0;
for(int i=1;i<=n;i++) {
sum+=i;
}
cout<<"Sum: "<<sum<<endl;
return 0;
}

Output:

Areas of Rectangles.

#include <iostream>

using namespace std;

int main()
{
double w1, l1, w2, l2;
cout<<"Enter the first recangle width: "<<endl;
cin >> w1;
while(w1 < 0) {
cout<<"Invalid input. ";
cout<<"Enter the first recangle width: "<<endl;
cin >> w1;
}
cout<<"Enter the first recangle lenght: "<<endl;
cin >> l1;
while(l1 < 0) {
cout<<"Invalid input. ";
cout<<"Enter the first recangle lenght: "<<endl;
cin >> l1;
}
cout<<"Enter the second recangle width: "<<endl;
cin >> w2;
while(w2 < 0) {
cout<<"Invalid input. ";
cout<<"Enter the second recangle width: "<<endl;
cin >> w2;
}
cout<<"Enter the second recangle lenght: "<<endl;
cin >> l2;
while(l2 < 0) {
cout<<"Invalid input. ";
cout<<"Enter the second recangle lenght: "<<endl;
cin >> l2;
}
double recangle1 = l1 * w1;
double recangle2 = l2 * w2;
if(recangle1 > recangle2) {
cout<<"First traingle area is greater"<<endl;
} else if(recangle2 > recangle1) {
cout<<"Second traingle area is greater"<<endl;
} else {
cout<<"Both Rectangles are same"<<endl;
}

return 0;
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote