In: Computer Science
A Pythagorean triplet is a set of positive integers (x, y, z) such that x2 + y2 = z2. Write an interactive script that asks the user for three positive integers (x, y, z, in that order). If the three numbers form a Pythagorean triplet the script should a) display the message ‘The three numbers x, y, z, form a Pythagorean triplet’ b) plot the corresponding triangle using red lines connecting the triangle edges. Hint: place the x value on the x-axis and the y value on the y-axis starting at the origin, i.e., start at point (0,0) If the three numbers do not form a Pythagorean triplet the script a) should compute the area of the triangle using Heron’s formula: Area = sqrt( p(p-x)(p-y)(p-z)) where p= (x+y+z)/2 display the message ‘The three numbers x, y, z, correspond to a triangle with are area area’ Make sure that in your script you write comments for the steps above, e.g. % ---- comment ---
`Hey,
Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.
clc
clear all
close all
format long
x=input('Enter x: ');
y=input('Enter y: ');
z=input('Enter z: ');
if(x^2+y^2==z^2)
disp('The three numbers x, y, z, form a Pythagorean triplet');
x1=linspace(0,x,100);
y1=linspace(0,0,100);
plot(x1,y1);
hold on;
y1=linspace(0,y,100);
x1=linspace(0,0,100);
plot(x1,y1);
x1=linspace(0,x,100);
y1=linspace(y,0,100);
plot(x1,y1);
else
disp('The three numbers x, y, z, do not form a Pythagorean triplet')
end
p=(x+y+z)/2;
A=sqrt(p*(p-x)*(p-y)*(p-z));
fprintf('The three numbers x, y, z, correspond to a triangle with are area %f\n',A);
Kindly revert for any queries
Thanks.