# Square Root in C Language

### Introduction

In mathematics, the square root of a number is a value that, when multiplied by itself, gives the original number. It is denoted by the symbol v. In computer programming, the square root function is a built-in function in many programming languages, including the C programming language. In this article, we will explore how to find the square root of a number using the C language.

### Square Root in C Language

The C programming language provides a built-in function called sqrt() to find the square root of a number. The sqrt() function is defined in the math.h header file. This function takes a single argument, which is the number whose square root is to be found, and returns the square root of that number.

The syntax of the sqrt() function is as follows:

`double sqrt(double x);`

Here, the argument x is the number whose square root is to be found, and the function returns the square root of x as a double-precision floating-point value.

Let's look at some examples to understand how to use the sqrt() function in C.

### Example 1: Find the square root of a number using sqrt()

``````#include <stdio.h>
#include <math.h>
int main() {
double num, result;
printf("Enter a number: ");
scanf("%lf", &num);
result = sqrt(num);
printf("The square root of %.2lf is %.2lf\n", num, result);
return 0;
}``````

Output:

``````Enter a number: 12
The square root of 12.00 is 3.46``````

In this example, we have used the sqrt() function to find the square root of a number entered by the user. We have first included the math.h header file, which contains the definition of the sqrt() function. We have then declared two double-precision variables, num and result. We have used the printf() and scanf() functions to take input from the user and display the result.

We have then used the sqrt() function to find the square root of the number entered by the user and stored the result in the variable result. We have used the printf() function to display the result to the user.

### Example 2: Find the square root of multiple numbers using sqrt()

``````#include <stdio.h>
#include <math.h>
int main() {
int i, n;
double num[100], result;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (i = 0; i< n; i++) {
scanf("%lf", &num[i]);
}
printf("The square roots of the elements are:\n");
for (i = 0; i< n; i++) {
result = sqrt(num[i]);
printf("%.2lf ", result);
}
return 0;
}``````

Output:

``````Enter the number of elements: 5
Enter the elements: 10 25 36 49 86
The square roots of the elements are:
3.16 5.00 6.00 7.00 9.27``````

In this example, we have used the sqrt() function to find the square roots of multiple numbers entered by the user. We have declared an array of double-precision variables called num, which can hold up to 100 elements. We have used the printf() and scanf() functions to take input from the user and display the result.

We have then used a for loop to iterate through the array and find the square root of each element using the sqrt() function. We have used the printf() function to display the results to the user.

### Handling Negative Numbers

The sqrt() function in C returns a NaN (Not a Number) if the argument passed to it is negative. This is because the square root of a negative number is a complex number, which cannot be represented as a real number.

Example:

``````#include <stdio.h>
#include <math.h>
int main() {
double num, result;
printf("Enter a number: ");
scanf("%lf", &num);
result = sqrt(num);
if (isnan(result)) {
printf("Invalid input. Cannot find square root of a negative number.\n");
} else {
printf("The square root of %.2lf is %.2lf\n", num, result);
}
return 0;
}``````

Output:

``````Enter a number: -45
Invalid input. Cannot find square root of a negative number.``````

In this example, we have first taken input from the user using the scanf() function. We have then used the sqrt() function to find the square root of the number entered by the user and stored the result in the variable result.

We have used the isnan() function to check if the result is NaN. If the result is NaN, we have displayed an error message to the user, indicating that the input is invalid. If the result is not NaN, we have displayed the result to the user.

### Conclusion:

The square root function is an essential mathematical function that is used in various applications. In the C programming language, the sqrt() function is used to find the square root of a number. The sqrt() function takes a single argument, which is the number whose square root is to be found, and returns the square root of that number as a double-precision floating-point value.

When using the sqrt() function, it is essential to handle negative numbers properly. If the argument passed to the sqrt() function is negative, it returns a NaN, indicating that the square root of a negative number is not defined. It is always a good practice to check for NaN values when using the sqrt() function.

Overall, the sqrt() function in C is a powerful tool for finding the square root of a number. By understanding how to use the sqrt() function, you can perform complex mathematical operations in your C programs with ease.