Begin19. The coordinates of two opposite vertices of the rectangle are given: \((x_1, y_1), (x_2, y_2)\). The sides of the rectangle are parallel to the coordinate axes. Find the perimeter and area of this rectangle.

Solution in Python 3:

import random

x1,x2 = random.sample(range(-10, 10), 2)
y1,y2 = random.sample(range(-10, 10), 2)

print("(x1, y1): ({0},{1})".format(x1, y1))
print("(x2, y2): ({0},{1})".format(x2, y2))
side1 = abs(x1 - x2)
side2 = abs(y1 - y2)
P = 2 * (side1 + side2)
S = side1 * side2
print("Side 1: ", side1)
print("Side 2: ", side2)
print("Perimeter: ", P)
print("Area: ", S)

Solution in C++:

#include <iostream>
#include <cmath>
using namespace std;
int main(){
double x1,y1,x2,y2,a,b,p,s;
cout << "Enter the coordinate of x1: ";
cin >> x1;
cout << "Enter the coordinate of y1: ";
cin >> y1;
cout << "Enter the coordinate of x2: ";
cin >> x2;
cout << "Enter the coordinate of y2: ";
cin >> y2;
a = abs(x1-x2);
b = abs(y1-y2);
p = (a+b)*2;
s = a*b;
cout << "Perimeter: " << p << endl;
cout << "Area: " << s;
return 0;
}