Exact square root | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Exact square root

#include <iostream> using namespace std; int main() { int num, sqr,i; float temp, temp1,temp2; cout<<"Enter the no: "; cin>>num; for(i=1;i<num/2;i++){ sqr=i*i; if(sqr==num){ cout<<"Square root of "<<num<<" is "<<i; break; } if(sqr>num){ i=i-1; //temp2=(2*i); //temp1=(num-i*i); // temp=i+ temp1 /temp2; temp=i+(num-i*i)/(2*i); cout<<"Square root of "<<num<<" is "<<temp ; break; } } return 0; } Problem here is that if I will use temp1 and temp2 in temp then it return exact square root of any number but if I run temp without using temp1 and temp2 then it doesn't return exact value . Then exactly where is the problem

23rd Dec 2023, 6:46 AM
Om Yele
Om Yele - avatar
5 Answers
+ 3
It depends of how works the compiler, that means the order of operations and used memory. This can be noticeable with very precise calculations.
23rd Dec 2023, 7:16 AM
JaScript
JaScript - avatar
+ 2
Bro you are using builtin function but I don't want to use built-in function. I want to write my own function
31st Dec 2023, 5:34 AM
Om Yele
Om Yele - avatar
+ 1
Builtin functions are better practice actually but u must know how they actually
31st Dec 2023, 5:39 AM
Manuphile
Manuphile - avatar
+ 1
It actually uses binary search
31st Dec 2023, 5:40 AM
Manuphile
Manuphile - avatar