How to calculate square root in C++
If you are a student, you might be interested or given the task to write a C++ program to calculate the square root of a given number. In that case, you might not be allowed to use the built-in function in the C++ library.
In this tutorial, I will show you examples of calculating the square root by writing your logic as well as the built-in functions provided in the library. These four ways are:
- Using C++ sqrt() built-in function
- C++ sqrtl() built-in function
- C++ sqrtl() built-in function
- Writing a custom function
Let us discuss these ways in detail with an example of each in the section below.
First way: using C++ sqrt() function
The <cmath> library in C++ contains many functions related to math.
For example, exp(), pow(), floor() and many others.
It makes life quite easier to perform mathematical operations for C++ programmers.
For calculating the square root of the given number, the <cmath) has sqrt() function.
Syntax of C++ sqrt function
So, the sqrt function takes double as input argument.
- It must be a positive number, otherwise, an error is generated.
- The sqrt function returns the square of the given number as the return value.
An example of sqrt() function
In the program below, we will calculate the square root of the number 625 and display it on the screen by using the sqrt function under <cmath> library.
#include <iostream> #include <cmath> using namespace std; int main() { //Declaring variables with double data type double x = 625; double y; //Using the sqrt function y = sqrt(x); //Display the value on screen cout << "Square root of " << x <<" = " << y ; return 0; }
Output:
What if we enter a negative number?
As we ran the above program and assigned a negative number (-625 in that case) to calculate the square root, see the output yourself:
#include <iostream> #include <cmath> using namespace std; int main() { //Declaring variables and assigning a negative value double x = -625; double y; //Using the sqrt function y = sqrt(x); //Display the value on screen cout << "Square root of -625" << x <<" = " << y ; return 0; }
Output:
You can see the result is nan.
The example of C++ sqrt() with user input
In this program, a user is asked to enter a positive number for what he/she wants to get the square root:
#include <iostream> #include <cmath> using namespace std; int main() { //Declaring variables with double data type double x, y; //Taking User input with a message cout << "Enter the value to get Square Root? "; cin >>x; //Using the sqrt function to calculate square root y = sqrt(x); //Display the value on screen cout << "Square root of " << x <<" = " << y ; return 0; }
Output:
Second way – Using sqrtf() function example
The math library has another function sqrtf() that also calculates the square root of a number.
Syntax:
The returns the square root of the given number as a float.
An example of sqrtf() function
As you execute the program below by copying/pasting into your C++ editor, it will ask you to enter a float number. As you enter the number, it will display the square root.
#include <iostream> #include <cmath> using namespace std; int main() { //Declaring variables with double data type float x, y; //Taking User input in float cout << "Enter a float number to calculate square root? "; cin >>x; //Using the sqrtf function to calculate square root in float y = sqrtf(x); //Display the value on screen cout << "Square root of " << x <<" = " << y ; return 0; }
A sample output:
Third way: Using sqrtl function
The sqrtl() is also available in the <cmath> library of C++.
Syntax:
An example of sqrtl() function
This program asks the user to enter a long double number for which he/she wants to calculate the square root:
#include <iostream> #include <cmath> using namespace std; int main() { //Declaring variables with long int data type long double x, y; //Taking User input in long double cout << "Enter a long double number to calculate square root? "; cin >>x; //Using the sqrtl function to calculate square root in long double y = sqrtl(x); //Display square root in long double cout << "Square root of " << x <<" = " << y ; return 0; }
A sample output:
Writing a custom function example
Now, let us show you how to write your custom function for calculating the square root of the given number.
The code:
#include <iostream> using namespace std; int calcSqRt(int x) { //A C++ function to calculate square root of a given number float temp, result; int inp_y; inp_y = x; result = inp_y / 2; temp = 0; while(result != temp){ temp = result; // Calculating the square root logic result = ( inp_y/temp + temp) / 2; } cout << "Square root of " << inp_y <<" = " << result ; return (result); } int main() { int num; cout << "Please enter the number to return square root: " ; cin >> num; //An if condition to check the input number is +ve //if -ve, function will not be called if (num>0){ calcSqRt(num); } else{ cout <<"Number must be positive!"; } }
Sample output 1:
Sample output 2
Sample output for a negative number