Square root without using math.h

I am trying to find square root of number without using math.h library but i am still unable to find its logic.
Though i have found that how to find Factorial and Power of some number without using math.h Library!
Is there anyone who can tell me the logic that how to find square root of a number without using math.h library.
you can also send me an E-Mail on this address:"saqibkamal1994@gmail.com" or "saqibkamal1994@yahoo.com".Thanks!
Thanks for your information but i am not able to execute these methods in programming!
The Babylonian method looks pretty simple to program.

go back to the link that Peter87 just posted.
(btw, Tnx Peter87 !, lots of good info here)

the answer is already written for you !

short isqrt(short num) {
short res = 0;
short bit = 1 << 14; // The second-to-top bit is set: 1L<<30 for long

// "bit" starts at the highest power of four <= the argument.
while (bit > num)
bit >>= 2;

while (bit != 0) {
if (num >= res + bit) {
num -= res + bit;
res = (res >> 1) + bit;
}
else
res >>= 1;
bit >>= 2;
}
return res;
}

Under the heading of EXAMPLES !

Too, look at the same link , under External links, where you'll find some great help including a white paper on how to do SQR(roots) using subtraction only. - makes for good weekend reading.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
//  main.cpp
//  squareroots
//
//  Created by Home on 2/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <math.h>  
#include <exception> 

using namespace std;

int main(){
    // required variables
    float square_root = 0, estimation = 1;  
    int cycle=50; 
    // try/throw/catch to handle exceptions
    try {
    cout << "enter in a number: ";
        cin >> square_root;
        if (square_root<=0){
            throw 0;
        }
    }
    // catches exceptions of 0 or any character
    catch (int invalid_value){
        cout << "invalid number entered...";
        return 0; 
    } 
    // estimates the value of the square root of number entered
    while(cycle>0){
        estimation = ( estimation + ( square_root / estimation ) ) / 2; 
        --cycle; 
    }
    //outputs the estimated value and the sqrt() function in math.h
    cout << "estimation is: " << estimation << '\n';
    cout << "actual answer is: " << sqrt(square_root);
    return 0;
}

enjoy finding out out how that works :).
Last edited on
I found another way guys!
#include<iostream.h>
#include<conio.h>
void main()
{
int number;
float i=0.1,sqrt=0;
while(i*i<=number)
{
i+=0.1;
}
sqrt=i;
cout<<sqrt;
getch();
}
Yeh... that doesn't work. Try 15 with that and then with mine. Urs will come up wrong and will take ALOT longer to process for numbers over 10. Plug like 80 into urs. It will take ages. Mine will run in under a second. Please use actual mathmatics instead of just throwing EVERY number at it until one works. That is why they teach math in school.
Last edited on
Topic archived. No new replies allowed.