Square Root in C
Overview
In mathematics, a number 'x' is called the square root of the number 'n' if 'x' and 'n' if the square of the number 'x' is equal to 'n'. Mathematically, .
In the C programming language, we have many functions present in various header files. One such function to calculate the square root of a number is present in the library 'math.h' is sqrt() function.
In this article, we will discuss the function sqrt function in C programming language.
Syntax of the sqrt() Function
sqrt function in C programming language (sqrt()) is a function which is used to find the square root of a number. It is present in math.h header file.
It takes a single argument which is the number of which the square root is to be calculated. The return type of the sqrt() function is double.
Below is the syntax of how to use a sqrt function in C programming language.
Syntax of the pow() Function
Pow in C is a function in the C programming language which is used to compute power of a number. It is present in 'math.h' header file.
It takes two arguments which are two numbers 'num1' and 'num2' and calculates the result as . The return type of the pow() function is double.
Below is the syntax of how to use a pow() function in C programming language.
Syntax of the log2() Function
Log2 in C is a function in the C programming language that is used to compute the log of a number to the base 2. It is present in math.h header file.
It takes a single number as an argument and computes the log of the argument number to base 2.
The return type of the log2() function is double.
Below is the syntax of how to use a log2() function in C programming language.
Examples
Given below are some different ways to find the square root of a number.
- Program to get the square root of a number using the sqrt function in C programming language
Given below is the program to get the square root of a number using sqrt() function.
- Program to take a number from the user and to get the square root
Given below is the program to take a number from the user and get the square root.
- Program to find the square root using user-defined function
Given below is the program to find the square root of numbers up to two decimal places.
- Program to get the square root of a number using the pow() function
The power function takes two arguments, the first is the number of which the power is to be calculated and the second is the number that denotes the power to be raised.
We know that the square root of a number is equal to the power of that number raised to 0.5. Hence, we can use 0.5 as the second argument of the power function in order to calculate the square root of the number.
Given below is the code to get the square root of a number using the pow() function.
- Program to get the square root of a number without using the sqrt function in C programming language As discussed above, we can use the power function instead of square root function to calculate square root of a number. Given below is the code for the same.
Also, as discussed above, we can find the square root of a number using simple loops. Given below is the program to find the square root of numbers up to two decimal places.
- Program to get the square root of a number by using log2()
Before calculating the square root of a number, we must find a mathematical relation between the sqrt and the log2 function.
We know that 'x' is said to be the square root of a number 'n' if,
Taking log2() both the sides, we get,
Given below is the program to get the square root of a number by using log2().
Conclusion
- A number 'x' is called as the square root of the number 'n' if 'x' and 'n' if the square of the number 'x' is equal to 'n'.
- sqrt function in C programming language (sqrt()) is a function that can compute the square root of a number.
- Square root of a number can also be calculated using a different functions such as power function (pow()) and log function (log2()).
- All the function i.e. sqrt(), pow() and log2() are present in the math.h header file.