How to do Square Roots in C++ - WITHOUT "SQRT" Command

Started by JKozzy2 pages

Originally posted by jerlark386
I'm sorry. I was just messing with Jkozzy's mind.
[code]
cout <<"Enter the first side of the right triangle\n";//assign first side
cin >> integer1;//redefine integer1
cout <<"Enter the second side\n";//assign second side
cin >> integer2;//redefine integer2
integer3=(integer1*integer1)+(integer2*integer2);
//finds hypotenuse squared
integer3=(integer4*integer4);//finds hypotenuse
cout <<"The hypotenuse of the right triangle is "<<integer4<<endl;

return(0);//indicate that the program has ended successfully
}

[/code]

Its a really minor error. You can fix it with this.

[code]
cout <<"Enter the first side of the right triangle\n";//assign first side
cin >> integer1;//redefine integer1
cout <<"Enter the second side\n";//assign second side
cin >> integer2;//redefine integer2
integer3=(integer1*integer1)+(integer2*integer2);
//finds hypotenuse squared
//integer3=(integer4*integer4);//finds hypotenuse//get rid of this line
cout <<"The hypotenuse of the right triangle is the square root of"<<integer3<<endl;

return(0);//indicate that the program has ended successfully

}

[/code]
So you can't use the sqrt or the breakdown version in my other post. Then don't. Without those, you're pretty much out of luck anyway. Besides your teach would'nt liked all those gotos anyway. If you give an answer that sounds good, then it'll probably be better than nothing. Your teacher will laugh, but your answer will be 'valid'.

Who does'nt love a good linguistic hack.

💃

integer3 is still C squared though.
Pythagoran Theorem is a squared plus b squared equals c squared. a and b are already squared, and the answer you show is c squared.... the square root of c needs to be found for the program to be complete 😬

🙂

Oh, is that all you wanted?

Simple math then

a squared + b squared = c squared

Then your program has show the root of c square.

My question is why square the first two values in the first place when,

a * b = your anwser that you want to square

So in that case, all you have to do is this

[code]
cout <<"Enter the first side of the right triangle\n";//assign first side
cin >> integer1;//redefine integer1
cout <<"Enter the second side\n";//assign second side
cin >> integer2;//redefine integer2
integer3=integer1 * integer2;
//finds hypotenuse squared
cout <<"The hypotenuse of the right triangle is"<<integer3<<endl;
return(0);//indicate that the program has ended successfully
[/code]

Thats saves you alot of unneccessary coding. And the best part is it still generates the answer you want.

That wouldn't work. Take the 3-4-5 triangle.

3squared + 4squared = 5squared
9+16=25

If I input 3 as A and 4 as B, I want the program to spit out 5 as the hypotenuse.

What you have there is
3*4=5, which doesn't work, because 3*4=12.

Yeah, square roots don't work like that.

Hmmm. I cannot think of anyway to bypass that off the top of my head...

Originally posted by JKozzy
That wouldn't work. Take the 3-4-5 triangle.

3squared + 4squared = 5squared
9+16=25

If I input 3 as A and 4 as B, I want the program to spit out 5 as the hypotenuse.

What you have there is
3*4=5, which doesn't work, because 3*4=12.

Oh, too bad. I thought you were working with just right triangles. Then it looks like you will have to do a little extra writing. Just write a sub to check inputs for 3 and 4.