{"id":7202,"date":"2023-11-01T12:20:14","date_gmt":"2023-11-01T06:50:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=7202"},"modified":"2023-11-10T12:12:04","modified_gmt":"2023-11-10T06:42:04","slug":"python-program-to-find-the-square-root","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-square-root\/","title":{"rendered":"Python Program to Find the Square Root | Square Root in C++"},"content":{"rendered":"
Given a number ,the task is to find the square root of the given number.<\/p>\n
Note : <\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 ().<\/p>\n That’s what there is to it! You can now measure square roots with math.sqrt().<\/p>\n sqrt() has a simple user interface.<\/p>\n 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.<\/p>\n The square root of x as a floating point number is returned by sqrt(). This will be 4.0 in the case.<\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can calculate square root of a number easily by using ** operator.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n In C++, the sqrt() function returns the square root of a number.<\/p>\n The <cmath> header file defines this feature. A domain error occurs when a negative argument is passed to the sqrt() function.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The sqrt() function from the cmath (complex math) module will be used in this program.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n We must use the eval() function instead of float() if we want to take a complex number as input directly, such as 4+5j<\/p>\n In Python, the eval() method can be used to transform complex numbers into complex objects.<\/p>\n 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 …<\/p>\nSquare root exists for even complex numbers too.<\/pre>\n
number = 16<\/pre>\n
The Square root of the given number 16 = 4.0<\/pre>\n
number = 4 + 3 j<\/pre>\n
The Square root of the given number (4+3j) = (2.1213203435596424+0.7071067811865476j)<\/pre>\n
number = 12<\/pre>\n
The Square root of the given number 12 = 3.4641016151377544<\/pre>\n
Program to Find Square Root of a Number<\/h2>\n
\n
Method #1:For Positive numbers in Python using math.sqrt function<\/h3>\n
# importing math module\r\nimport math\r\n# given number\r\nnumber = 16\r\n# finding square root\r\nnumberSqrt = math.sqrt(number)\r\n# printing the square root of given number\r\nprint(\"The Square root of the\u00a0given\u00a0number\", number, \"=\", numberSqrt)\r\n<\/pre>\n
The Square root of the given number 16 = 4.0<\/pre>\n
Method #2:Using ** operator<\/h3>\n
# importing math module\r\nimport math\r\n# given number\r\nnumber = 16\r\n# finding square root\r\nnumberSqrt = number**0.5\r\n# printing the square root of given number\r\nprint(\"The Square root of the given number\", number, \"=\", numberSqrt)\r\n<\/pre>\n
The Square root of the given number 16 = 4.0<\/pre>\n
Method #3:Using sqrt function in C++<\/h3>\n
\nA single non-negative argument is passed to the sqrt() function.<\/p>\n#include <cmath>\r\n#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{ \/\/ given number\r\n float number = 16, numberSqrt;\r\n \/\/ calculating the square root of the given number\r\n numberSqrt = sqrt(number);\r\n \/\/ printing the square root of the given number\r\n cout << \"The square root of the given number \" << number\r\n << \" = \" << numberSqrt;\r\n\r\n return 0;\r\n}<\/pre>\n
The square root of the given number 16 = 4<\/pre>\n
Method #4:Using cmath.sqrt() function in python<\/h3>\n
# importing cmath module\r\nimport cmath\r\n# given complex number\r\nnumber = 4 + 3j\r\n# finding square root of the given complex number\r\nnumberSqrt = cmath.sqrt(number)\r\n# printing the square root of given number\r\nprint(\"The Square root of the given number\", number, \"=\", numberSqrt)\r\n<\/pre>\n
The Square root of the given number (4+3j) = (2.1213203435596424+0.7071067811865476j)<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n