Given a number ,the task is to find the square root of the given number.
Note :
Square root exists for even complex numbers too.
Examples:
Example1:
Input:
number = 16
Output:
The Square root of the given number 16 = 4.0
Example2:
Input:
number = 4 + 3 j
Output:
The Square root of the given number (4+3j) = (2.1213203435596424+0.7071067811865476j)
Example3:
Input:
number = 12
Output:
The Square root of the given number 12 = 3.4641016151377544
Program to Find Square Root of a Number
- For Positive numbers in Python using math.sqrt function
- Using ** operator
- Using sqrt function in C++
- Using cmath.sqrt() function in python
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
Method #1:For Positive numbers in Python using math.sqrt function
The math module in Python’s standard library can be used to solve math problems in code. It has a lot of useful functions like remainder() and factorial() (). sqrt, the Python square root function, is also included ().
That’s what there is to it! You can now measure square roots with math.sqrt().
sqrt() has a simple user interface.
It only requires one parameter, x, which represents the square for which you are attempting to calculate the square root (as you might recall). This would be 16 in the previous case.
The square root of x as a floating point number is returned by sqrt(). This will be 4.0 in the case.
We store the number in number and use the sqrt function to find the square root in this program. This program can be used for any positive real number. However, it does not deal for negative or complex numbers.
Below is the implementation:
# importing math module import math # given number number = 16 # finding square root numberSqrt = math.sqrt(number) # printing the square root of given number print("The Square root of the given number", number, "=", numberSqrt)
Output:
The Square root of the given number 16 = 4.0
Method #2:Using ** operator
We can calculate square root of a number easily by using ** operator.
Below is the implementation:
# importing math module import math # given number number = 16 # finding square root numberSqrt = number**0.5 # printing the square root of given number print("The Square root of the given number", number, "=", numberSqrt)
Output:
The Square root of the given number 16 = 4.0
Method #3:Using sqrt function in C++
In C++, the sqrt() function returns the square root of a number.
The <cmath> header file defines this feature.
A single non-negative argument is passed to the sqrt() function.
A domain error occurs when a negative argument is passed to the sqrt() function.
Below is the implementation:
#include <cmath> #include <iostream> using namespace std; int main() { // given number float number = 16, numberSqrt; // calculating the square root of the given number numberSqrt = sqrt(number); // printing the square root of the given number cout << "The square root of the given number " << number << " = " << numberSqrt; return 0; }
Output:
The square root of the given number 16 = 4
Method #4:Using cmath.sqrt() function in python
The sqrt() function from the cmath (complex math) module will be used in this program.
Below is the implementation:
# importing cmath module import cmath # given complex number number = 4 + 3j # finding square root of the given complex number numberSqrt = cmath.sqrt(number) # printing the square root of given number print("The Square root of the given number", number, "=", numberSqrt)
Output:
The Square root of the given number (4+3j) = (2.1213203435596424+0.7071067811865476j)
Note:
We must use the eval() function instead of float() if we want to take a complex number as input directly, such as 4+5j
In Python, the eval() method can be used to transform complex numbers into complex objects.
Related Programs:
- python program to find a pair with the given sum in an array
- python program to find the missing number in an array list
- python program to find the odd occurring element in an array list
- python program to find the second largest number in a list
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to find the sum of elements in a list recursively
- python program to find the largest number in a list