Answers for "how to take square root in c++"

C++
1

square root c++

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

/*
square root of a number
*/

int main(){
float num, raiz;
printf("enter a number: t");
scanf("%f",&num);
raiz = sqrt(num);
printf("The square root of %f is: %f.n", num, raiz);
system("pause");
return 0;    
}
Posted by: Guest on April-16-2021
0

how to make a square root function in c++ without stl

double sqrt(double number)
{
    double error = 0.00001; //define the precision of your result
    double s = number;

    while ((s - number / s) > error) //loop until precision satisfied 
    {
        s = (s + number / s) / 2;
    }
    return s;
}
Posted by: Guest on June-05-2021
0

how to make a square root function in c++ without stl

double SqrtNumber(double num)
{
    double lower_bound=0; 
    double upper_bound=num;
    double temp=0;

    while(fabs(num - (temp * temp)) > SOME_SMALL_VALUE)
    {
           temp = (lower_bound+upper_bound)/2;
           if (temp*temp >= num)
           {
                   upper_bound = temp;
           }
           else
           {
                   lower_bound = temp;
           }
    }
    return temp;
 }
Posted by: Guest on June-05-2021
0

how to make a square root function in c++ without stl

#include <iostream>
    using namespace std;

    double SqrtNumber(double num)
    {
             double lower_bound=0; 
             double upper_bound=num;
             double temp=0;                    /* ek edited this line */

             int nCount = 50;

        while(nCount != 0)
        {
               temp=(lower_bound+upper_bound)/2;
               if(temp*temp==num) 
               {
                       return temp;
               }
               else if(temp*temp > num)

               {
                       upper_bound = temp;
               }
               else
               {
                       lower_bound = temp;
               }
        nCount--;
     }
        return temp;
     }

     int main()
     {
     double num;
     cout<<"Enter the numbern";
     cin>>num;

     if(num < 0)
     {
     cout<<"Error: Negative number!";
     return 0;
     }

     cout<<"Square roots are: +"<<sqrtnum(num) and <<" and -"<<sqrtnum(num);
     return 0;
     }
Posted by: Guest on June-05-2021

Browse Popular Code Answers by Language