+92 332 4229 857 99ProjectIdeas@Gmail.com

How to calculate the square root (C++)


Squareroot
sqrroot() calculates the square root of the given value and it takes 1 parameter:
val takes the value whose square root is desired.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"

using namespace std;

double sqrroot(double val)
{
 double i=0;
 double temp1,temp2;
      
       while(i*i<=val)
              i=i+0.1;

       temp1=i;
   
       for(int j=0;j<15;j++)
       {
              temp2=val;
              temp2=temp2/temp1;
              temp2=temp2+temp1;
              temp2=temp2/2;
              temp1=temp2;
       }
return temp2;
}

int main()
{
       cout << sqrroot ( 2.0 ) << endl ;
       cout << sqrroot ( 3.0 ) << endl ;
       cout << sqrroot ( 5.0 ) << endl ;
       cout << sqrroot ( 9.0 ) << endl ;

       _getche();
       return 0;
}

Output
1.41421
1.73205
2.23607
3

0 comments: