sqrt() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

sqrt() in C++ is used to return the square root of any number. It is defined in the cmath header file. It takes in a non-negative number as an argument and returns the square root of that number.

We can state mathematically : sqrt(x) = √x.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Syntax of sqrt() in C++

The syntax of sqrt in C++ is as follows:

Here x is a non-negative number whose square root in C++ is to be calculated.

Parameters of sqrt() in C++

Following are the parameters of sqrt() function in C++ :

  • x : A non-negative number whose square root is to be calculated.
Sharpen Your Fundamentals with Free Learning

Return Value of sqrt() in C++

The return value of sqrt() in C++ is the square root of the given number x. This square root returned is of datatype double.

Exceptions for sqrt() in C++

Let us now go through the exceptions of sqrt() in C++ :

  • sqrt() in C++ is only defined for non-negative numbers. This means negative numbers are out of the domain for the sqrt() method. Thus, domain error will occur if negative numbers are passed as arguments to sqrt in C++.
  • If we use the sqrt() method in C++ without any argument, it will throw an error stating that "no matching function for call to sqrt()"

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example of sqrt() Function

Let us now go through an example and the code for sqrt() in C++:

We will see what sqrt() in C++ returns if we pass zero, a perfect square number, a not-perfect number, a decimal number, and a negative number as a parameter.

Code :

Output :

As we can see, we get the square root of the number whenever we pass a non-negative argument. The square root is not defined for negative numbers, so we get nan when we pass -19.

What is sqrt() in C++ ?

Let us once again revisit the points we studied about sqrt in C++ :

  • The sqrt() function in C++ is defined in the cmath header file and is used to return the square root of a number.
  • The sqrt() method takes a non-negative number as an argument and returns its square root in datatype double.
  • sqrt() in C++ is not defined for negative numbers. Thus, it gives domain error whenever negative numbers are passed as an argument.
  • sqrt() method can not be called without using any argument, else it will throw an error.
  • sqrt() in C++ is defined for double values only, but it works fine with integral values, too, since the integer values are implicitly converted into type double.

square root of x

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example :

In the first example, we shall use sqrt with double arguments, and in the second, we shall use sqrt with integral arguments.

1. Sqrt in c++

Let us calculate the sqrt of a double value, say 98.56:

Output:

Explanation:
As we can see, the return value is of type double.

2. Sqrt in c++ with Integral Argument

Let us see what happens if we pass an integral value, say 98, to the sqrt() method.

Output:

Explanation:
Though sqrt() in C++ is defined for double only, it did not through an error when we passed an integral value. This is because C++ implicitly converted the integral value to a double value.

The return value is of type double.

Take the first step towards becoming a C++ expert. Enroll in our free C++ course and bring your software ideas to life.

Conclusion

  • sqrt() in C++ returns the square root of a non-negative number. It gives a domain error if negative numbers are passed.
  • It is defined in the cmath header file.
  • The return value is of type double.