C++ sqrt()

MamtaWardhani's avatar
Published Sep 8, 2022Updated Jul 23, 2025
Contribute to Docs

C++ sqrt() function returns the square root of a number. This mathematical function is part of the C++ standard library and is defined in the <cmath> header file.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax of C++ sqrt()

sqrt(num)

Parameters:

  • num: A floating-point number (double, float, or long double) for which the square root is to be calculated

Return value:

The sqrt() function returns the square root of the given argument as a floating-point value. Its return type matches the type of the argument (e.g., float, double, or long double).

Note: If a negative argument is passed to sqrt(), a domain error occurs and the function returns NaN (Not a Number).

Example 1: Basic Usage of sqrt() function

This example demonstrates the basic usage of the sqrt() function with different numerical values:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
// Calculate square root of different numbers
cout << "Square root of 16: " << sqrt(16) << endl;
cout << "Square root of 25: " << sqrt(25) << endl;
cout << "Square root of 2: " << sqrt(2) << endl;
cout << "Square root of 100.5: " << sqrt(100.5) << endl;
return 0;
}

This example results in the following output:

Square root of 16: 4
Square root of 25: 5
Square root of 2: 1.41421
Square root of 100.5: 10.025

Note: Output may vary slightly depending on platform-specific floating-point rounding.

Example 2: Distance Calculation using C++ sqrt()

This example shows how sqrt() is used to calculate the distance between two points using the Pythagorean theorem:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
// Calculate distance between two points (x1, y1) and (x2, y2)
double x1 = 1.0, y1 = 2.0;
double x2 = 4.0, y2 = 6.0;
// Apply distance formula: sqrt((x2-x1)^2 + (y2-y1)^2)
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
cout << "Distance between points (" << x1 << ", " << y1 << ") and ("
<< x2 << ", " << y2 << ") is: " << distance << endl;
return 0;
}

This example results in the following output:

Distance between points (1, 2) and (4, 6) is: 5

Codebyte Example: Using sqrt() to Solve Quadratic Equation

This example demonstrates using sqrt() to solve a quadratic equation using the quadratic formula:

Code
Output
Loading...

Frequently Asked Questions

1. What happens if I pass a negative number to sqrt()?

Passing a negative number to sqrt() results in a domain error and returns NaN (Not a Number).

2. What header file do I need to include to use sqrt()?

You need to include the <cmath> header file to use the sqrt() function.

3. What is the return type of the function sqrt() in C++?

The sqrt() function returns a floating-point value of the same type as the input parameter. If you pass a float, it returns a float; if you pass a double, it returns a double, and if you pass a long double, it returns a long double.

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours