1#include <math.h>
2
3//get square root of a number "b"
4int main(){
5 int a = 2; //declare number you want to take square root of
6 int sqrtNum = sqrt (a); //assign the sqrt value to a variable
7 cout << sqrtNum << endl;
8 return 0;
9}
1#include <stdlib.h>
2#include <stdio.h>
3#include <math.h>
4
5/*
6square root of a number
7*/
8
9int main(){
10float num, raiz;
11printf("enter a number: \t");
12scanf("%f",&num);
13raiz = sqrt(num);
14printf("The square root of %f is: %f.\n", num, raiz);
15system("pause");
16return 0;
17}
1#include <iostream>
2#include <cmath>
3using namespace std;
4
5int main()
6{
7 int x = 625;
8 int result = sqrt(x);
9 cout << "Square root of " << x << " is " << result << endl;
10 return 0;
11}
1#include <math.h>
2
3double sqrt(double x) {
4 if (x <= 0)
5 return 0; // if negative number throw an exception?
6 int exp = 0;
7 x = frexp(x, &exp); // extract binary exponent from x
8 if (exp & 1) { // we want exponent to be even
9 exp--;
10 x *= 2;
11 }
12 double y = (1+x)/2; // first approximation
13 double z = 0;
14 while (y != z) { // yes, we CAN compare doubles here!
15 z = y;
16 y = (y + x/y) / 2;
17 }
18 return ldexp(y, exp/2); // multiply answer by 2^(exp/2)
19}
20