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.