Using C Using the Pythagorean Theorem calculate the hypotenu

Using C++:

Using the Pythagorean Theorem, calculate the hypotenuse of a right triangle given the length of the two shorter sides. Be certain to use the square and squareRoot functions (You will need to change the square function to use floating point numbers though.) Ask the user if s/ he wants to keep entering data; if \"Yes\" then continue prompting and calculating. Assume no sides are negative.

float squareRoot (float s) {
float xn;
if (s == 0.0) {
return 0.0;
}
xn = s/2.0;
int counter = 1;
while (counter <= 10) {
xn = (xn + (s/xn))/2.0;
counter = counter + 1;
}

return xn;
}

and

int square (int what) {
int answer = what * what;
return answer;
}

Solution

#include <iostream>
#include <cmath>
using namespace std;
float squareRoot (float s) {
float xn;
if (s == 0.0) {
return 0.0;
}
xn = s/2.0;
int counter = 1;
while (counter <= 10) {
xn = (xn + (s/xn))/2.0;
counter = counter + 1;
}

return xn;
}

float square (float what) {
float answer = what * what;
return answer;
}

int main()
{
   char ans;
   do
   {          
       float base,perpendicular;
       cout<<\"Enter length of Base : \";
       cin>>base;
       float base_sq=square(base);
       cout<<\"Enter length of Perpendicular : \";
       cin>>perpendicular;
       float perpendicular_sq=square(perpendicular);
      
       float sum=base_sq+perpendicular_sq;
       float hypotenuse=squareRoot(sum);
      
       cout<<\"Hypotenuse length : \"<<hypotenuse;
      
       cout<<\"\ Do you want to enter the sides length again (y|Y) ?\";
       cin>>ans;
   }while(ans==\'Y\' || ans==\'y\');
   return 0;
}

Using C++: Using the Pythagorean Theorem, calculate the hypotenuse of a right triangle given the length of the two shorter sides. Be certain to use the square a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site