 Sqrt c что это
Перейти к содержимому

# Sqrt c что это

## sqrt() in C

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

### 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.

### 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

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 .

### 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.

## sqrt in C Function sqrt in C returns the square root of a number. To use it, include the «math.h» header file in the program.

Declaration: double sqrt(double);

### Square root in C using sqrt function

int main ( )
<
double n , result ;

printf ( «Enter a number to calculate its square root \n » ) ;
scanf ( «%lf» , & n ) ;

result = sqrt ( n ) ;

printf ( «Square root of %.2lf = %.2lf \n » , n , result ) ;

We use «%.2lf» in printf function to print two decimal digits.

Output of program: ### C program to find square root of numbers from 1 to 100

int main ( )
<
int c ;

// The function sqrt expects a double data type argument so we convert int to a double.

for ( c = 1 ; c <= 100 ; c ++ )
printf ( «√%d = %lf \n » , c , sqrt ( ( double ) c ) ) ;

## C sqrt()

The sqrt() function computes the square root of a number.

### Function prototype of sqrt()

The sqrt() function takes a single argument (in double ) and returns its square root (also in double ).

The sqrt() function is defined in math.h header file.

To find the square root of int , float or long double data types, you can explicitly convert the type to double using cast operator.

You can also use the sqrtf() function to work specifically with float and sqrtl() to work with long double type.

## Sqrt Function in C

The term sqrt() stands for “square root” which calculates the square root of any positive or negative number. The sqrt() in C takes a single value as an input and calculates its square root as an output. It takes any positive or negative value. You can use the “math.h” library to implement the function in the code. The sqrt() function also computes the square root of float, integers, or long double data types. All you need to do is to use a cast operator to convert the type of double.

To work with float numbers simply use the “sqrtf()” function and if you want to deal with long double type then use “sqrtl()”.If you enter any negative value as an input, then the function sqrt() in C shows a domain error. Come let’s check how the sqrt() function works in the C language. Here we will elaborate sqrt() by using the GNU GCC compiler in Windows 10.

### Example 1

Our first example is to calculate the square root of a long float number using the sqrt() function. Now let’s check how the program works. Open the GNU GCC compiler in Windows 10 and select a new empty file from the file menu. To create a new file, you can also use a keyboard shortcut ‘Ctrl+Shift+N.’ Now the file has been successfully opened in the GNU Editor. Well, it’s time to write a C code to elaborate sqrt(). We will be discussing the simplest and easiest way to calculate a square root using sqrt() in C language. So, you have to add the ‘stdio.h’ and ‘math.h’ libraries. We can then define a main function in the program. After this, we use a declaration method to declare a variable.

Then we have defined two functions i.e., printf() and scanf(). Then function prinf() in C is used to print the value entered by a user and the function scanf() takes a long float (%lf) number from the user. Then we have used an sqrt() function to calculate a square root of a number entered by the user. After you successfully write the code, now it’s time to save your code file with the ‘.cc’ extension as below. The file name may be different in your illustration. Now, build and run the file or simply use the “F9” shortcut key to check the output of a C code of sqrt(). Follow the given steps in the console and then click the “Enter” key. ### Example 2

Our next program is to calculate the square root of a number from 1 to 10 using the sqrt() function. The program takes a number ranging from 1 to 10 as an input and displays a square root of that number as an output. Let’s move to the GNU GCC compiler in Windows 10 and select a new empty file or use the same file “Program1.cc”. We use the same code file “Program1.cc” and made changes to it. Here, we use the same ‘math.h’ and ‘stdio.h’ standard libraries just like we did in the previous example. In this illustration, we use an integer variable and a “for” loop to print all the numbers from 1 to 10. We use the function sqrt() in printf() function which takes double data type as an input so we adjust the integer variable to double. The printf() function shows all the numbers and their square roots. Again save the “Program1.cc” file for further execution. Then again, build and run the code or simply use the F9 key to check the output of a C code of sqrt(). After compiling the above program, you will get the desired result. ### Example 3

Our next program is to calculate the square root of a positive number using the sqrt() function. Here is another example that takes a positive integer number as an input and displays a positive integer number as an output. Let’s move to the GNU GCC compiler in Windows 10 and select a new empty file or use the same file “Program1.cc”. We use the same code file “Program1.cc” and made changes to it. We used the same ‘math.h’ and ‘stdio.h’ standard libraries as the main function just like we did in the above example. In this illustration, we have defined two integer variables. One variable is utilized for input and the other is used for output. We have defined two functions i.e., printf() and scanf().

Then function prinf() in C is used to print the value entered by a user and the function scanf() takes a number from the user to show its square root. Then, we use an sqrt() function to calculate a square root of a number inserted by the user. At last, we use a printf() function which shows the calculated square root value. Note: If your code is not executed then abort the previous action and then click on the build and run tab for further execution. Again, build and run the code or simply use the “F9” shortcut key to check the output of a C code of getch(). ### Conclusion

In this tutorial, we discussed the usage and importance of the sqrt() function and its implementation using the GCC compiler. We have discussed three different examples for the understanding of the sqrt() function in the C programming language. You can now easily use integers and long floating-point numbers with the sqrt() function in the C programming language.