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

The sqrt() function in C returns the square root of the number. It is defined in the math.h header file.

Transform Your Career

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

Syntax for sqrt() in C

The syntax for the sqrt function in C is given below:

Parameters for sqrt() in C

The sqrt() function takes a single number(in double) as the input.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Return Values for sqrt() in C

The sqrt() function returns a double number value as the output. However, if the input is negative, it returns -nan. To understand why that happens, see Exceptions.

Exceptions for sqrt() in C

When the input number is negative, the sqrt function in C returns -nan(not a number). This is because the square root of a negative number is imaginary, and the sqrt() function doesn't support an imaginary number. This is a domain error, i.e., the input is not within the function's domain.

Output

Scaler Placement Report and Statistics

₹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 to Illustrate the Working of sqrt() Function in C

Output

What is sqrt() Function in C?

The sqrt() function in C returns the square root of the number. It is defined in the math.h header file. It takes the input(in double) and returns the output(in double). If the input is negative, it returns a domain error-nan.

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

How to Use the sqrt() Function in C?

  • If we want to use the sqrt function in C for int, float, etc., we can typecast these into double values and then pass them into the function.
  • If the input is not a double value (e.g., int, float, etc.), it is automatically typecasted into a double. The sqrt() function in C is defined such that if the input is not a double value(e.g., int, float, etc.), it is typecasted into double, and then its square root is calculated.
  • We can also use the sqrtf() for float values and sqrtl() for long double values.

Note: Since the sqrt() function returns double values, it is also prone to small roundoff errors.

More Examples of sqrt() in C

  • Program to get the square root of a number using the sqrt() function in C.

Output

Explanation The program declares three variables a,b, and c and computes the square root of these numbers using the sqrt() function. Note that variables a and b are integers. When they are passed into the sqrt() function, it typecasts them into a double value.

  • Program to take a number from the user and get its square root using the sqrt() function in C.

Output

Explanation

In this program, we take a non-negative number from the user and compute its square root using the sqrt() function. If the user passes a negative number, it will result in a domain error. See Exception for more details.

  • Program to get the square root of a number using the pow() function.

Output

Explanation

In this program, we calculate the square root of a number using the pow() function. The pow() function takes two parameters, the base, and the exponent. Therefore, when we compute pow(a, 0.5), it is the same as the square root of a.

  • Program to get the square root of a number using a user-defined function without using the sqrt() function.

Output

Explanation

In this program, we write a user-defined function to calculate the square root of the number. The user enters a number, a, and we pass this number to the function squareRoot(). We store a / 2 in j. Initially, i = 0. We then assign j to i and update the value of j. This goes on until j is not equal to i. This method is called the Newton Raphson Method.

Conclusion

  • The sqrt function in C returns the square root of the number.
  • It takes a single, double parameter and returns the output as a double.
  • It returns a domain error if the input is negative.

See Also

Some functions similar to the sqrt function are as follows.