i need help

Write your question here.
so i made a program of calculating a side using the pythagorean theory and can anyone tell me if i could've done something easier because i think it took to much time (i'm a complete beginner)

#include <iostream>
#include <istream>
#include <cmath>
#include <string>
using namespace std;

int main() {
cout << "are you calculating a hipotenuse? " << endl;
string anwser;
cin >> anwser;

if (anwser == "yes"){
int x;
cout << "type the first length: " << endl;
cin >> x;
int first_sum;
first_sum = x*x;
int y;
cout << "type the second length: " << endl;
cin >> y;
int second_sum;
second_sum = y*y;
int p;
p = first_sum + second_sum;
double z;
z = sqrt(p);
cout << "the length of the hypotenuse is: " << z << endl << endl;
} else if (anwser == "no") {
int x;
cout << "type the length of the hypotenuse: " << endl;
cin >> x;
int first_sum;
first_sum = x*x;
int y;
cout << "type the second length: " << endl;
cin >> y;
int second_sum;
second_sum = y*y;
int p;
p = first_sum - second_sum;
double z;
z= sqrt(p);
cout << "the length of that side is: " << z << endl << endl;

}
else{
cout << "please type yes or no " << endl << endl;
return main();
}
string b;
cout << "do you want to continue?" << endl;
cin >> b;
if (b == "yes") {
return main();
}
else if (b == "no"){
cout << "goodbye! " << endl;
return 0;
}


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   double a, b, c;
   char ans;

   cout << "Input two sides: ";   cin >> a >> b;
   cout << "Are you calculating a hypotenuse (y/n): ";   cin >> ans;

   if ( ans == 'y' || ans == 'Y' ) c = sqrt( a * a + b * b );
   else                            c = sqrt( abs( a * a - b * b ) );
   cout << "Third side is " << c << '\n';
}


Input two sides: 5 12
Are you calculating a hypotenuse (y/n): y
Third side is 13


Input two sides: 5 13
Are you calculating a hypotenuse (y/n): n
Third side is 12
wow thats way easier
Registered users can post here. Sign in or register to post.