- 1 1. How to Calculate Square Roots in C: Overview and the Basic sqrt Function
- 2 2. Basic Method for Calculating Square Roots
- 3 3. Applications: Various Use-Cases for Square Root Calculations
- 4 4. Square Root Calculations Without the Standard Library
- 5 5. Benefits and Limitations of Square Root Calculations
- 6 6. Summary
1. How to Calculate Square Roots in C: Overview and the Basic sqrt Function
The C programming language includes the sqrt function in its standard library, allowing you to easily compute the square root of a numeric value. This enables efficient calculation of square roots, which can otherwise become complex. In this article you will learn how to use the sqrt function, explore various application scenarios, and even see how to implement your own algorithm—making this article useful for beginners through advanced programmers.
2. Basic Method for Calculating Square Roots
First, let’s explain the basic method of computing square roots in C.
Overview and Usage of the sqrt Function
The sqrt function is one of the functions provided in the math.h library, and it computes the square root of an arbitrary numeric value. The function prototype is as follows:
#include <math.h>
double sqrt(double x);This function returns the square root of the argument x that is passed in.
Basic Usage Example
Here is a program that calculates and prints the square root of a number entered by the user.
#include <stdio.h>
#include <math.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num < 0) {
printf("Cannot compute square root of a negative number.\n");
} else {
printf("Square root: %lf\n", sqrt(num));
}
return 0;
}In this program the user inputs a number, then the program computes and displays its square root. If a negative number is entered, an error message is displayed and the program terminates.
Handling Negative Numbers and Important Notes
The sqrt function is not defined for negative arguments, so passing a negative number will cause an error. Therefore you need to include logic to check whether the input value is negative. If you need to compute square roots of negative numbers (complex numbers), use the csqrt function from the complex.h library.
3. Applications: Various Use-Cases for Square Root Calculations
The sqrt function is frequently used in numerical analysis and scientific computing. Here we introduce representative application examples.
Calculating Euclidean Distance
Euclidean distance represents the distance between two points in 2-D or 3-D space, and it is calculated using the square root. For example, in a 2-D plane for two points (x1, y1) and (x2, y2), the Euclidean distance is calculated as follows:
#include <stdio.h>
#include <math.h>
int main() {
double x1 = 1.0, y1 = 2.0;
double x2 = 4.0, y2 = 6.0;
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
printf("Euclidean distance: %lf\n", distance);
return 0;
}Use in Graphics Programming
The sqrt function is also used when calculating the length of a vector. For example, for a 2-D vector (vx, vy), its length is computed as follows:
#include <stdio.h>
#include <math.h>
int main() {
double vx = 3.0, vy = 4.0;
double length = sqrt(vx * vx + vy * vy);
printf("Vector length: %lf\n", length);
return 0;
}Square Roots of Complex Numbers
Since the standard sqrt function cannot compute the square root of complex numbers you must use the csqrt function from the complex.h library. Here is a code example that computes the square root of a complex number:
#include <stdio.h>
#include <complex.h>
int main() {
double complex z = -4.0 + 0.0 * I;
double complex result = csqrt(z);
printf("Square root: %.2f + %.2fi\n", creal(result), cimag(result));
return 0;
}4. Square Root Calculations Without the Standard Library
It is also possible to compute square roots without using the standard sqrt function, by implementing your own algorithm. Here we introduce an implementation example using Newton’s method.
Custom Implementation Using Newton’s Method
Newton’s method (the Newton–Raphson method) is a well-known technique in numerical computing to find roots including square roots. Below is an example of computing a square root using Newton’s method.
#include <stdio.h>
double mySqrt(double num) {
double x = num;
double dx;
if (num == 0) return 0;
while (1) {
dx = (x * x - num) / (2.0 * x);
if (fabs(dx) < 0.00001) break;
x -= dx;
}
return x;
}
int main() {
double num = 9.0;
printf("Square root: %lf\n", mySqrt(num));
return 0;
}This code computes the square root of a given number using Newton’s method. It repeats the loop until the condition is met and gradually approaches the solution.

5. Benefits and Limitations of Square Root Calculations
Using the sqrt function offers many benefits, but you also need to understand some limitations.
Advantages of the sqrt Function
- Provided by the standard library: no additional installation required, environment-independent.
- Efficiency: optimized for numerical computation, so processing speed is fast.
- Precision: accuracy is guaranteed for floating-point computations.
Limitations and Countermeasures of the sqrt Function
- Constraint with negative numbers: calculating the square root of a negative number triggers an error. If you need square roots of complex numbers, use the
csqrtfunction fromcomplex.h. - Floating-point precision: when computing extremely small or large values, errors may occur. In such cases you need to devise algorithmic adjustments.
6. Summary
In this article we covered how to calculate square roots in C, starting from the standard library sqrt function, then introduced application examples such as Euclidean distance and graphics programming. We also explained a custom square-root calculation using Newton’s method, thereby covering a variety of methods and their applications.
Square-root computation is one of the basic numeric-processing tasks in C, but its range of application is very broad.


