# Find Square Root Using Babylonian Method in C++

Saad Aslam Sep-11, 2022 C++ C++ Math

Using the Babylonian approach, which will be explained in this article, we will learn how to determine the square root of an integer in C++ programming language.

## An Overview of the Babylonian Method in C++

The Babylonian technique for determining the square root of a number is derived from one of the numerical methods, which in turn is derived from the Newton-Raphson approach for the solution of non-linear equations.

The procedure may be summarized as a variation of the following algorithm.

1. First, we randomly choose a positive number and get it as near the root as possible.
2. We start by assigning the value one to a variable by saying `y = 1`.
3. Follow the instructions in the next two stages until you reach the point when you have the answer.
• Find the average of `x` and `y`, then calculate an approximation for the root.
• Set `y = n/x.`

Suppose we continue repeating the previous section’s procedures in a loop. In that case, we will eventually arrive at the square root of the number itself while utilizing the Babylonian technique to solve the problem.

## Implement the Babylonian Method to Find the Square Root in C++

Let us look at an example that uses a separate class to apply the Babylonian approach to finding the square root of a number.

To begin, we will import the necessary libraries for the application.

``````#include<iostream>
#include<math.h>
#include<stdio.h>
``````

Develop a float type method called `sqRootBySaad` that accepts a `sqNumber` parameter with a `float` datatype value.

``````float sqRootBySaad(float sqNumber) {}
``````

In the body of this method, you should first set the values of three variables: `value,` `guess,` and `constantValue.` The value allocated to the `value` variable will be the number supplied to this as an argument.

The value of the `guess` variable is set to `one,` and the `constantValue` is set to `zero.`

``````float value = sqNumber;
float guess = 1;
float constantValue = 0;
``````

After this, we apply a `while` loop to check if the `value - guess` value is greater than the `constantValue`. Then we will calculate the average of both the `value` and `guess` values, and the `sqNumber` will be divided by `value`.

After this, we use a `while` loop to check whether the value of `value - guess` is larger than the value of `constantValue.` If it is, we will compute the average of both the `value` and the `guess` values.

In addition, we will divide the `sqNumber` by the computation that we have just finished and save it in the `value` variable.

``````while (value - guess > constantValue)
{
value = (value + guess) / 2;
guess = sqNumber / value;
}
``````

The `sqRootBySaad` will return the `value` variable.

``````return value;
``````

Now that we have developed the procedure for finding the square root, we need to define a variable named `anyNumber` with the type int in the function called `main().` Show a message to the user asking them to submit the number so you can calculate its square root, and then take the user’s input for the number.

Now, call the method `sqRootBySaad` and provide the previously stored number in the variable `anyNumber.`

``````int main()
{
int anyNumber;
cout << "Enter any number to find square root: ";
cin >> anyNumber;
cout << "Square root using Babylonian method is: " << sqRootBySaad(anyNumber);
return 0;
}
``````

Full Source Code:

``````#include<iostream>
#include<math.h>
#include<stdio.h>

using namespace std;

{
float value = sqNumber;
float guess = 1;
float constantValue = 0;

while (value - guess > constantValue)
{
value = (value + guess) / 2;
guess = sqNumber / value;
}
return value;
}

int main()
{
int anyNumber;
cout << "Enter any number to find square root: ";
cin >> anyNumber;
cout << "Square root using Babylonian method is: " << sqRootBySaad(anyNumber);
return 0;
}
``````

Output:

``````Enter any number to find square root: 25
Square root using Babylonian method is: 5
``````

Let us look at an additional example that applies the Babylonian approach to finding the square root of a number.

Here, we initialize three variables `value`, `guess,` and `sqNumber` of datatype `double.`

Show a message to the user asking them to submit the number so that we may calculate the square root of the number, and then we input the value that the user provides.

``````double value, guess, sqNumber;

cout << "Please enter a number to find square root using the Babylonian method: ";
cin >> value;
``````

After dividing the number by two, we store the result in the variable referred to as `guess.` Implement a `for` loop with an iteration count of ten, and continue doing so until we reach the desired result.

Within the `for` loop, divide the `value` that the user has input by the `guess` value, and then save the result in the variable `sqNumber.` To update the value of the `guess` variable, you need to compute the average values contained in the `guess` and `sqNumber` variables.

``````   guess = value / 2.0;

for(int n = 0; n <= 10; n++)
{
sqNumber = value/guess;
guess = (guess + sqNumber)/2;
}
``````

At last, we print the `guess` value, the number obtained by taking the square root of the value the user has input.

``````cout << "The square root of " << value << " is: " << guess << endl;
``````

Full Source Code:

``````#include<iostream>

using namespace std;

int main()
{
double value, guess, sqNumber;
cout << "Please enter a number to find square root using the Babylonian method: ";
cin >> value;

guess = value / 2.0;

for(int n = 0; n <= 10; n++)
{
sqNumber = value/guess;
guess = (guess + sqNumber)/2;
}
cout << "The square root of " << value << " is: " << guess << endl;
}
``````

Output:

``````Please enter a number to find square root using the Babylonian method: 25
The square root of 25 is: 5
``````

I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.

## Related Article - C++ Math

• Calculate Exponent Without Using pow() Function in C++
• Intersection of Ray and Plane in C++
• C++ Cube Root
• Magic Square Problem in C++
• Division in C++