Find the Square Root Without Using the SQRT Function in C++

The sqrt() function is a built-in C++ function that calculates the square root of a number. It accepts one argument n and returns the square root of n.

But did you know that we can find the square root of a number in C++ without using the sqrt() function? In this article, we will explain how to do this.

Steps to Find the Square Root Without Using the sqrt Function in C++

  • The first step is to find half of the number. For example, if we want to see the square root of 16, then we will store 8 in a variable called sqrt.
  • The second step is to divide by 2. For example, if we want to find the square root of 16, we will divide 8 by 2 and store 4 in a temp variable called temp. We can do this because dividing by two is equivalent to multiplying by one-half.
  • The last step is to compute the square root operation by applying the loop. We will loop until the sqrt variable differs from temp.
  • During this time, we will keep updating the temp’s value with the previous sqrt value and continue in this manner. When the loop is completed, we will be eligible to print the square root of the number.

Example Code:

Solve an Equation with Square Root ...
Solve an Equation with Square Root - Grade 11
#include <iostream>
#include<math.h>
using namespace std;

int main()
{
int y=25;
float x =0 ;
double i;
cout<<"Let's find the Square Root of " << y;

while (i < y)
{
x = x + 1;
i = x*x;
if(y == i)
{
cout<<"\nThe square root of 25 is " << x;
break;
} } }

Click here to check the working of the code as mentioned above.

Related Article - C++ Math

  • C++ Cube Root
  • Find Square Root Using Babylonian Method in C++
  • Magic Square Problem in C++
  • Division in C++