Finding square root without using sqrt function

0 votes

I tried to incorporate the technique I had discovered for calculating the square root without utilising the sqrt function into programming. 

I finally have this C++ code that runs.

 #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 number\n";
     cin>>num;

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

     cout<<"Square roots are: +"<<sqrtnum(num) and <<" and -"<<sqrtnum(num);
     return 0;
     }

The current issue is initialising nCount in the declaratione's number of iterations ( here it is 50). 

In contrast, getting the square root of 15625 requires more than 50 iterations, therefore the value of temp would be returned after 50 iterations. For instance, calculating the square root of 36 requires 22 iterations, so there is no problem. 

Please offer a solution.

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
4,447 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
0 answers

get absolute value without using abs function nor if statement

How might I obtain an integer's absolute ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
1,222 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
2,514 views
0 votes
0 answers

Program to calculate square root c++

Making a C++ application to determine a number's square root.  The built-in arithmetic operator "sqrt" is not used in this application.  There are two variables: one for the user-inputted integer and the other for the number's square root.  This software does not function very well, and I am certain that there is a more effective method to accomplish it: Here is my full code: #include <iostream> using namespace ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
502 views
0 votes
1 answer

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
886 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
897 views
0 votes
1 answer

Function default argument value depending on argument name in C++ [duplicate]

When the function is called with no argument for the corresponding parameter, the default argument is evaluated.  In a default argument, a parameter must not appear as a potentially evaluated expression.  A function's parameters declared before a default argument are in scope and can obscure the namespace and class member name. It provides the following example: int h(int a, ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
594 views
0 votes
1 answer

Python math module

pow is built into the language but ...READ MORE

answered Nov 24, 2018 in Python by SDeb
• 13,300 points
801 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
2,924 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
518 views
0 votes
1 answer

lower_bound == upper_bound

Lower bound: the initial greater-or-equal element. Upper bound: ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
1,047 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP