What's Here?
- Members: 148,851
- Replies: 504,953
- Topics: 79,531
- Snippets: 2,661
- Tutorials: 705
- Total Online: 2,164
- Members: 75
- Guests: 2,089
|
This code shows how to find out the square root of a number without using the sqrt() function. The result obtained by both functions are the same and hence can also be used as an alternative sqrt() function.
|
Submitted By: born2c0de
|
|
Rating:
 
|
|
Views: 32,678 |
Language: C++
|
|
Last Modified: August 20, 2005 |
Snippet
/* Code written by Sanchit Karve
A.K.A born2c0de
Contact Me at born2c0de AT hotmail.com
20 August, 2005
*/
#include <iostream>
#include <math.h>
using namespace std;
float sqroot(float m)
{
float i=0;
float x1,x2;
while( (i*i) <= m )
i+=0.1;
x1=i;
for(int j=0;j<10;j++)
{
x2=m;
x2/=x1;
x2+=x1;
x2/=2;
x1=x2;
}
return x2;
}
int main()
{
cout<<"Enter a Number:";
int no;
cin>>no;
cout<<"Square Root using sqroot()= "<<sqroot(no)<<endl
<<"Square Root using sqrt() = "<<sqrt(no);
return 0;
}
Copy & Paste
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|